flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Usefulls macros

Author
Thread Post new topic Reply to topic
rydemstorm



Joined: 31 Oct 2012
Posts: 15
rydemstorm
Example call:
call proc,eax,bx,word 123,dword [var]
Code generated:
push eax
push bx
push word 123
push dword [var]
call proc
Example declaration:
proc:
SET ecx,8,dx,12,si,14,ebx,16
...
ret
Code generated:
proc:
push ebp
mov ebp,esp
mov ecx,[ebp+8]
mov dx,[ebp+12]
mov si,[ebp+14]
ebx,[ebp+16]
pop ebp
...
ret

Source Macros:
macro call proc*,[par]
{
common
if par eq
call proc
else
common
local ..override, ..count
..count=0
forward
push par
virtual at 0
push par
load ..override from 0
end virtual
if ..override <> 66h
..count=..count+2
else
..count=..count+4
end if
common
call proc
add esp,..count
end if
}
macro SET [reg,seg]
{
common
push ebp
mov ebp,esp
forward
mov reg,[ebp+seg]
common
pop ebp
}
Post 07 Nov 2012, 16:06
View user's profile Send private message Reply with quote
rydemstorm



Joined: 31 Oct 2012
Posts: 15
rydemstorm
Correction:
Example call:
call proc,eax,bx,word 123,dword [var]
Code generated:
push eax
push bx
push word 123
push dword [var]
call proc
add esp,12;it was forget in a first sample
Post 07 Nov 2012, 18:46
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
rydemstorm, you better use [code][/code] tags in your posts.
Post 07 Nov 2012, 18:57
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
rydemstorm



Joined: 31 Oct 2012
Posts: 15
rydemstorm
Code:
macro call proc*,[par]
{
 common
              if par eq
                   call proc
           else
                        common
                              local ..override, ..count
                           ..count=0
                   forward
                             push par
                            virtual at 0
                                        push par
                                    load ..override from 0
                              end virtual
                         if ..override <> 66h
                                  ..count=..count+2
                           else
                                        ..count=..count+4
                           end if
                      common
                              call proc
                           add esp,..count
             end if
}
macro SET [reg,seg]
{
   common
              push ebp
            mov ebp,esp
 forward
             mov reg,[ebp+seg]
   common
              pop ebp
}
    

Sample:
Code:
call Proc1,eax,ebx,ax,dword 0xffff, dword [d]
        jmp $
       Proc1:
          SET eax,4,ebx,8,ecx,12,edx,16,esi,20,edi,24
         add eax,ebx
 ret
    


Description: Call sample
Filesize: 14.77 KB
Viewed: 1865 Time(s)

call.png


Post 07 Nov 2012, 23:25
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
rydemstorm,

Isn't that one of those 10k ways (of T. A. Edison fame) to do it wrong?

forward directive in call macroinstruction means first-to-last pushing order, yet add esp,..count is similar to cdecl calling convention.

Linear addresses slightly above 0x7c00 suggest that this is boot sector code (i.e. 16-bit, this hypothesis is supported by instructions' encoding length), then your offsets are outright wrong.

What is your goal? Compact code? No way. Fast code? Orthogonally no way.
Post 21 Nov 2012, 18:13
View user's profile Send private message Reply with quote
rydemstorm



Joined: 31 Oct 2012
Posts: 15
rydemstorm
I am working in real mode. Using QEMU to run my programs.
The idea is that some numbers of line can be compact employing macros, obtain some lighting in a source code.
Abstract some operations that can not be do it directly.
Code:
;Example: Compact code
push eax
push ebx
push ax
push dword 0xffff
push dword [d]
call Proc1
;All this line can be compact using a macro call
call Proc1,eax,ebx,ax,dword 0xffff, dword [d] 
;Example abstract
mov es, cs; this can not do it directly, 
;but using a macro I simulate it is possible (push cs, pop es)
;avoiding a programmer worry in questions like that
    
Post 21 Nov 2012, 21:43
View user's profile Send private message Reply with quote
uart777



Joined: 17 Jan 2012
Posts: 369
uart777
rydemstorm: Hi, your SET macro could replace the , between operands (a,b) with = and it could support m,m by using push-pop as in the following:

Code:
macro SET [p] {
forward
define ?s 0

match =0 a==b, ?s p \{
 if a eqtype [] & b eqtype [] ; m=m
  push dword b
  pop dword a
 else
  if b eq 0 & a in \ ; if r=0, use xor r, r
   <eax,ecx,edx,ebx,esi,edi>
    xor a, a
  else
   mov a, b
  end if
 end if
 define ?s 1
\}

if ?s eq ; match not processed?
 'Syntax error'
end if
} 
    


Quote:
"The idea is that some numbers of line can be compact employing macros"


Here's what I refer to as "ultra compact ASM..."

HL Example. Only 60 lines...

Code:
; rotate 'r'ight (90) or 'l'eft (270)

function rotate.image, image, way
locals x, y, w, h, p, s, col, row
let eax=[image], [s]=[?image.p+eax],\
 [w]=[?image.w+eax], [h]=[?image.h+eax]
try [p]=scratch [h], [w] ; w/h reversed
.if [way]='r' ; right, 90
  let eax=[h], eax--, [col]=eax
  .loop [y]=0 to [h]
    .loop [x]=0 to [w]
      let eax=[x], eax*[h], eax+[col], eax<<2,\
      eax+[p], edx=[y], edx*[w], edx+[x], edx<<2,\
      edx+[s], [eax]=[edx]
    .endl
    let [col]--
  .endl
.else.if [way]='l' ; left, 270
  let [col]=0
  .loop [y]=0 to [h]
    let eax=[w], eax--, [row]=eax
    .loop [x]=0 to [w]
      let eax=[row], eax*[h], eax+[col], eax<<2,\
      eax+[p], edx=[y], edx*[w], edx+[x], edx<<2,\
      edx+[s], [eax]=[edx], [row]--
    .endl
    let [col]++
  .endl
.end
end.scratch [image]
let eax=[image],\ ; adjust location
edx=[?image.x+eax], ecx=[w], ecx>>1, edx+ecx,\
ecx=[h], ecx>>1, edx-ecx, [?image.x+eax]=edx,\
edx=[?image.y+eax], ecx=[h], ecx>>1, edx+ecx,\
ecx=[w], ecx>>1, edx-ecx, [?image.y+eax]=edx
endf

function size.image, image, w, h
locals i, j, p, s, x1, y1, w1, h1, x2, y2
let eax=[image], [s]=[?image.p+eax],\
 [w1]=[?image.w+eax], [h1]=[?image.h+eax]
try [p]=scratch [w], [h]
; ratio=(size<<16)/size
let eax=[w1], eax<<16, eax/[w], [x1]=eax,\
 eax=[h1], eax<<16, eax/[h], [y1]=eax
.loop [i]=0 to [h]
  .loop [j]=0 to [w]
    ; offset=(n*ratio)>>16
    let eax=[j], eax*[x1], eax>>16, [x2]=eax,\
     eax=[i], eax*[y1], eax>>16, [y2]=eax
    ; destiny[(i*w2)+j]=source[(y2*w1)+x2]
    let eax=[i], eax*[w], eax+[j], eax<<2,\
     eax+[p], ecx=[y2], ecx*[w1], ecx+[x2],\
    ecx<<2, ecx+[s], [eax]=[ecx] ; *p=*s
  .endl
.endl
end.scratch [image]
endf

function size.image.p, image, p
locals w, h, iw, ih
let eax=[image],\
 [iw]=[?image.w+eax], [ih]=[?image.h+eax],\
 eax=[iw], eax*[p], ecx=100, eax/ecx, [w]=eax,\
 eax=[ih], eax*[p], ecx=100, eax/ecx, [h]=eax
size.image [image], [w], [h]
endf    


Disassembly

205 lines of pure ASM compared to only 60 lines using HL features...

Code:
push ebp
mov  ebp,esp
sub  esp,28h
mov  eax,dword ptr [ebp+8]
push dword ptr [eax]
pop  dword ptr [ebp-1Ch]
push dword ptr [eax+0Ch]
pop  dword ptr [ebp-10h]
push dword ptr [eax+10h]
pop  dword ptr [ebp-0Ch]
push dword ptr [ebp+10h]
push dword ptr [ebp+0Ch]
call 00403E00
mov  dword ptr [ebp-20h],eax
test eax,eax
je   00403EE7
mov  eax,dword ptr [ebp-10h]
shl  eax,10h
cdq
idiv eax,dword ptr [ebp+0Ch]
mov  dword ptr [ebp-18h],eax
mov  eax,dword ptr [ebp-0Ch]
shl  eax,10h
cdq
idiv eax,dword ptr [ebp+10h]
mov  dword ptr [ebp-14h],eax
push 0
pop  dword ptr [ebp-28h]
mov  ecx,dword ptr [ebp-28h]
cmp  ecx,dword ptr [ebp+10h]
jge  00403ECF
push 0
pop  dword ptr [ebp-24h]
mov  ecx,dword ptr [ebp-24h]
cmp  ecx,dword ptr [ebp+0Ch]
jge  00403ECA
mov  eax,dword ptr [ebp-24h]
imul eax,dword ptr [ebp-18h]
sar  eax,10h
mov  dword ptr [ebp-8],eax
mov  eax,dword ptr [ebp-28h]
imul eax,dword ptr [ebp-14h]
sar  eax,10h
mov  dword ptr [ebp-4],eax
mov  eax,dword ptr [ebp-28h]
imul eax,dword ptr [ebp+0Ch]
add  eax,dword ptr [ebp-24h]
shl  eax,2
add  eax,dword ptr [ebp-20h]
mov  ecx,dword ptr [ebp-4]
imul ecx,dword ptr [ebp-10h]
add  ecx,dword ptr [ebp-8]
shl  ecx,2
add  ecx,dword ptr [ebp-1Ch]
push dword ptr [ecx]
pop  dword ptr [eax]
inc  dword ptr [ebp-24h]
jmp  00403E7F
inc  dword ptr [ebp-28h]
jmp  00403E72
push 403DE0h
push dword ptr [ebp+8]
call 00403CE7
push 403DE0h
call 00403CA1
mov  esp,ebp
pop  ebp
ret  0Ch

push ebp
mov  ebp,esp
sub  esp,10h
mov  eax,dword ptr [ebp+8]
push dword ptr [eax+0Ch]
pop  dword ptr [ebp-8]
push dword ptr [eax+10h]
pop  dword ptr [ebp-4]
mov  eax,dword ptr [ebp-8]
imul eax,dword ptr [ebp+0Ch]
mov  ecx,64h
cdq
idiv eax,ecx
mov  dword ptr [ebp-10h],eax
mov  eax,dword ptr [ebp-4]
imul eax,dword ptr [ebp+0Ch]
mov  ecx,64h
cdq
idiv eax,ecx
mov  dword ptr [ebp-0Ch],eax
push dword ptr [ebp-0Ch]
push dword ptr [ebp-10h]
push dword ptr [ebp+8]
call 00403E22
mov  esp,ebp
pop  ebp
ret  8

push ebp
mov  ebp,esp
sub  esp,20h
mov  eax,dword ptr [ebp+8]
push dword ptr [eax]
pop  dword ptr [ebp-0Ch]
push dword ptr [eax+0Ch]
pop  dword ptr [ebp-18h]
push dword ptr [eax+10h]
pop  dword ptr [ebp-14h]
push dword ptr [ebp-18h]
push dword ptr [ebp-14h]
call 00403E00
mov  dword ptr [ebp-10h],eax
test eax,eax
je   00404068
cmp  dword ptr [ebp+0Ch],72h
jne  00403FC3
mov  eax,dword ptr [ebp-14h]
dec  eax
mov  dword ptr [ebp-8],eax
push 0
pop  dword ptr [ebp-1Ch]
mov  ecx,dword ptr [ebp-1Ch]
cmp  ecx,dword ptr [ebp-14h]
jge  00403FC1
push 0
pop  dword ptr [ebp-20h]
mov  ecx,dword ptr [ebp-20h]
cmp  ecx,dword ptr [ebp-18h]
jge  00403FB9
mov  eax,dword ptr [ebp-20h]
imul eax,dword ptr [ebp-14h]
add  eax,dword ptr [ebp-8]
shl  eax,2
add  eax,dword ptr [ebp-10h]
mov  edx,dword ptr [ebp-1Ch]
imul edx,dword ptr [ebp-18h]
add  edx,dword ptr [ebp-20h]
shl  edx,2
add  edx,dword ptr [ebp-0Ch]
push dword ptr [edx]
pop  dword ptr [eax]
inc  dword ptr [ebp-20h]
jmp  00403F88
dec  dword ptr [ebp-8]
inc  dword ptr [ebp-1Ch]
jmp  00403F7B
jmp  00404025
cmp  dword ptr [ebp+0Ch],6Ch
jne  00404025
mov  dword ptr [ebp-8],0
push 0
pop  dword ptr [ebp-1Ch]
mov  ecx,dword ptr [ebp-1Ch]
cmp  ecx,dword ptr [ebp-14h]
jge  00404025
mov  eax,dword ptr [ebp-18h]
dec  eax
mov  dword ptr [ebp-4],eax
push 0
pop  dword ptr [ebp-20h]
mov  ecx,dword ptr [ebp-20h]
cmp  ecx,dword ptr [ebp-18h]
jge  0040401D
mov  eax,dword ptr [ebp-4]
imul eax,dword ptr [ebp-14h]
add  eax,dword ptr [ebp-8]
shl  eax,2
add  eax,dword ptr [ebp-10h]
mov  edx,dword ptr [ebp-1Ch]
imul edx,dword ptr [ebp-18h]
add  edx,dword ptr [ebp-20h]
shl  edx,2
add  edx,dword ptr [ebp-0Ch]
push dword ptr [edx]
pop  dword ptr [eax]
dec  dword ptr [ebp-4]
inc  dword ptr [ebp-20h]
jmp  00403FE9
inc  dword ptr [ebp-8]
inc  dword ptr [ebp-1Ch]
jmp  00403FD5
push 403DE0h
push dword ptr [ebp+8]
call 00403CE7
push 403DE0h
call 00403CA1
mov  eax,dword ptr [ebp+8]
mov  edx,dword ptr [eax+4]
mov  ecx,dword ptr [ebp-18h]
sar  ecx,1
add  edx,ecx
mov  ecx,dword ptr [ebp-14h]
sar  ecx,1
sub  edx,ecx
mov  dword ptr [eax+4],edx
mov  edx,dword ptr [eax+8]
mov  ecx,dword ptr [ebp-14h]
sar  ecx,1
add  edx,ecx
mov  ecx,dword ptr [ebp-18h]
sar  ecx,1
sub  edx,ecx
mov  dword ptr [eax+8],edx
mov  esp,ebp
pop  ebp
ret  8    


However, it would be best to teach beginners pure ASM first with no HL features: invoke, proc, struct, etc.
Post 29 Nov 2012, 03:37
View user's profile Send private message Reply with quote
uart777



Joined: 17 Jan 2012
Posts: 369
uart777
If you are interested in writing your own HL macros, please check out my LANGUAGE.INC: http://board.flatassembler.net/topic.php?t=14768 This file is everything I've learned from the FASM forum over the years and is my way of giving back to the source of knowledge. Thanks to everyone who has posted macros.
Post 29 Nov 2012, 03:53
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.