News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

im confused..

Started by peterpants, October 05, 2011, 05:34:29 PM

Previous topic - Next topic

peterpants

hi asm masters, whats the right way to call a function with a byte argument, is it?
mov byte ptr [esp], al
or just..
push eax
im confused cause push eax is 4 bytes..

Vortex

#1
Hi Peter,

Welcome to the forum.

The stack is always 4 bytes aligned and you should do push eax.

peterpants

thank for your help asm master.

NoCforMe

Well, you could  do what you asked about:



MOV BYTE PTR [ESP],  AL



and it might even work. Two (at least two) problems with this, though:

  • The called  routine might expect the rest of the word on the stack to be zero, although if it really does take a BYTE argument it shouldn't.
  • The stack will be "unbalanced" (i.e., not pointing to where it should  before the call), so you'd have to move the stack pointer by hand:


SUB ESP, 2



Which is why we use PUSH instead, as it does all this in one operation.

(Note: I can't remember whether a PUSH increments or decrements the stack pointer. Oh, well.)

qWord

Quote from: NoCforMe on October 05, 2011, 07:00:52 PM

SUB ESP, 2

this would produce a stack misalignment -> sub esp,4
FPU in a trice: SmplMath
It's that simple!

NoCforMe

Arrrrgh! My bad. Shows my background as a DOS programmer. Old habits die hard.

xandaz

    so....and what if we push ax? it's still a valid argument right? it does:
mov word ptr [esp],ax
sub esp,2
~
    you just have to remember to pop a word or a 16b register. But feel free to explain because i don't think that i«m, understanding...

jj2007

Try your luck....
include \masm32\include\masm32rt.inc

.code
start: push ax
MsgBox 0, "Hello World", "Pushing ax is great:", MB_OK
pop ax
exit

end start

raymond

There are many things you simply cannot do in High Level Languages which you can easily do in assembly. It is thus not necessary to duplicate all HLL idiosyncracies in assembly.

For example, ALL arguments MUST be passed on the stack (either directly or as a pointer to memory) in HLL. They CANNOT be passed in a register. However, in assembly, arguments can be passed in registers.

mov al,arg
call function

And you write your function to retrieve the byte argument from the AL register (or whatever other register you decide to use). :clap:
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

xandaz

    ok i see your point jj. But nevertheless it works if you don't call the msgbox func.

hutch--

Ray has made the point clearly that you can use registers to pass data to a procedure in assembler. Now it is basically the case that ANYTHING you place in a register (or two) can be passed to a procedure and that includes BIT patterns, BYTE, WORD, DWORD and with 2 registers QUAD sized values.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

jj2007

Quote from: xandaz on October 05, 2011, 08:28:29 PM
    ok i see your point jj. But nevertheless it works if you don't call the msgbox func.

On Win XP SP3 I see a crippled MessageBox, a sign that inside the API the misaligned stack causes trouble. But technically speaking you can use any stack alignment - just balance it after use, and make sure you don't call a Windows API in between. Anyway, I'd call it bad practice, and never had a need for it :bg

hutch--

I agree with JJ here, use the native processor data size for the OS version and you get the maximum speed and proper stack alignment. There is nothing to gain by using smaller stack alignments but you can make a mess of it by doing so.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dedndave

even in those cases where a misaligned stack may work, it will slow things down terribly   :P
on a 32-bit system, the cpu likes to access data on 4-aligned boundries
this applies to all data, actually - not just the stack

NoCforMe

Quote from: dedndave on October 05, 2011, 11:05:08 PM
even in those cases where a misaligned stack may work, it will slow things down terribly   :P
on a 32-bit system, the cpu likes to access data on 4-aligned boundries
this applies to all data, actually - not just the stack

So one should put all one's QWORD data first in the data segment (followed by DWORDS and WORDS), correct?

The linker and loader ensure that all program segments are aligned on a [qword? page?] boundary, don't they?