but it doesn't run properly (but compiles fine)
I also tried putting WORD i as an argument:

Code:

void outputstuff(WORD i)
...

push i
call outputstuff
....

but that didn't work either. Any idea?

Also, the conditional jump gave me some trouble. It works fine now, but I want to say if i is greater than or equal to 256, quit, but jge just quit right away and jle quit at 129. what's with that?

Thanks

09-18-2008

matsp

You probably don't want to "pop" something inside a function, because there are other things pushed onto the stack between the call and the assembler in your function (almost always ebp, and possibly other registers).

Why not make "outputstuff" take an unsigned int argument (as that is what you are actually doing), and add 4 to esp after the call to outputstuff. Obviously, your first "jmp looping" is a waste of space - the next line is the label looping anyways.

As to your conditional jump, if you want an unsigned compare, use JAE.

--
Mats

09-18-2008

Elysia

You can't really get portable with assembly, so I'll throw in another unportable concept.
If you use Visual Studio, you can use __declspec(naked) to have the compiler strip the initialization code (and the "epilogue" code) from a function. That should allow you to pop whatever you just pushed on the stack.
But this is much more of a hack than real programming.
The real way would just be to add "WORD i" to the argument list and NOT pop the stack (but push the argument to the stack before calling).

09-18-2008

master5001

Judging by what the code is doing, he is probably trying to learn assembler....

09-18-2008

RobotGymnast

I'm in the process of learning assembly. And yeah I know it's not very good code, I was just trying to get it to work. thanks for your help, I've combined it with other answers

09-19-2008

master5001

This is why people never bet against me. I am good at pointing out the obvious.

09-19-2008

Dino

I tried compiling this (for fun) and "WORD" is undefined. I'm on a Mac. Is there an include I need for inline asm?

09-19-2008

master5001

Just typedef unsigned short as WORD, though it is not necessarily the best platform independant definition of "WORD." You should not need any specific includes to compile this code other than maybe windows.h... Which you obviously do not have.

09-19-2008

matsp

It appears that printf gets upset if the stack is not aligned properly to 4 bytes, so I found this to work:

I added the fflush to make sure I would see the output immediately, rather than at the very end of the program.

--
Mats

09-19-2008

CornedBee

Quote:

It appears that printf gets upset if the stack is not aligned properly to 4 bytes, so I found this to work:

And many things will be upset on x86-64 architectures if you don't align the stack to 16 bytes.

09-19-2008

matsp

Quote:

Originally Posted by CornedBee

And many things will be upset on x86-64 architectures if you don't align the stack to 16 bytes.

Ehm, but that responsibility lies in the callee, not the caller. The caller can pass a single argument (8 bytes) and not adjust the stack, but the called function needs to pad to the next lower 16 bytes after saving its registers.

In this case, the called function assumes that the stack is aligned to 4 bytes, and strange things happens [I have not figured out what goes wrong, just that it DOES].

--
Mats

09-19-2008

Elysia

Mats, if you don't mind...
I don't know what the instructions movzx and jae does. Can you explain their function?
Also you do "add word ptr i,1", as per the the OP, but hey, why not just stuff it into a register and use inc?

09-19-2008

foxman

MOVZX stands for something like move with zero extend. JAE is for jump above or equal.

Mats, if you don't mind...
I don't know what the instructions movzx and jae does. Can you explain their function?
Also you do "add word ptr i,1", as per the the OP, but hey, why not just stuff it into a register and use inc?

movzx - Move with Zero Extension: It loads the 16-bit value into a 32-bit register, and fills the remaining 16 bits with zero. It's counterpart for signed numbers is movsx, which fills the upper 16 bits with the content of bit 15 of the low 16 bits.

jae - Jump Above or Equal. It's the same as JGE but for unsigned numbers.

And I can't continue to use ECX after the call, since it's one of the registers that can be used in the callee without preservation, so it would require another load before inc can be used on it. Of course, I could have picked another register...

--
Mats

09-19-2008

Dino

Quote:

Originally Posted by matsp

It appears that printf gets upset if the stack is not aligned properly to 4 bytes, so I found this to work: