Passing 1 byte argument to function?

I want to create a function that takes 1 byte argument. But I read that in x86 I can only stack 2 or 4 bytes onto the stack. So, should I expect a 2 byte argument to be passed to my function and then retrieve my 1 byte? This is how to pass 1 byte argument to my function:

push WORD 123

      

+3


source to share


1 answer


The stack must be aligned. If you are running a 16-bit real-mode program, the stack must be 16-bit aligned. If you are making a 32-bit protected mode program, the stack must be 32-bit aligned.

But you don't need to pass exactly 1 byte to the function. Just hit 16/32 bits and only use the lowest ones in the function. Something like that:



use32
proc  MyFunc, .arg32, .arg16, .arg8
begin
       mov  eax, [.arg32]
       mov  bx, word [.arg16]
       mov  cl, byte [.arg8]
       ret
endp

Main:
       push  ecx   ; CL is arg8
       push  ebx   ; BX is arg16
       push  eax   ; EAX is arg32
       call  MyFunc

; Or shortly:
       stdcall MyFunc, eax, ebx, ecx

      

+2


source







All Articles