...

View Full Version : Assembly Language stack issue



buffcubby
10-10-2008, 03:50 PM
I'm trying to use stacks in this fibonacci program and I'm not doing it right and looking for suggestions because if i comment out the push/pop statements it still writes to the string correctly but if i push a different register it changes the output....or am i already doing it correctly?...

.data
count DWORD 12 ; Number of loops to perform
val1 DWORD ?
string byte (" "),0 ; use for the space between number

.code
main PROC
mov ecx, count ; loop counter
mov eax, 0
mov val1, 1


ADDLOOP: ; First time loop:
mov ebx, val1
mov val1, eax ; save current eax for our next addition
add eax, ebx
push eax
call DisplaySum
pop eax
loop ADDLOOP ; If not done, perform loop again (ecx is automatically decremented)
exit
main ENDP


DisplaySum PROC
call WriteInt ; Display number
mov edx, OFFSET string ; add space between the numbers
call WriteString
ret
DisplaySum ENDP

END main

oracleguy
10-10-2008, 07:01 PM
It has been a little while since I've done x86 assembly but why are you pushing/popping EAX around that call to DisplaySum? The call instruction will automatically push all the registers onto the stack and the ret pops them off. Why would you want to push any different registers? (You don't need to.)

This "WriteInt" procedure which you didn't include, does it print whatever the value of EAX is?

Trinithis
10-10-2008, 07:15 PM
call does NOT push all the regs on the stack, and ret does NOT pop all the regs. I know call pushes the IP, and I also think it pushes the CS. (And ret would pop the same ones). If ret is given an argument, it'll pop the corresponding count from the stack.

oracleguy
10-10-2008, 08:28 PM
call does NOT push all the regs on the stack, and ret does NOT pop all the regs. I know call pushes the IP, and I also think it pushes the CS. (And ret would pop the same ones). If ret is given an argument, it'll pop the corresponding count from the stack.

I stand corrected nevertheless, unless WriteInt and WriteString don't properly push/pop the registers they use, what I said about pushing/popping EAX still stands.

buffcubby
10-10-2008, 10:15 PM
My issue is that when I view the values of the ESP register it is decrementing it by 4 like it should for DWORDs and all the numbers are being pushed onto the stack correctly because I get the fibonacci numbers as output...the trouble is that the pop call should be popping them off in reverse order which it is not doing. The reason for the first DisplaySum call was to get the first 0 to show up for the fibonacci, otherwise it started with the number one, but nonetheless it still isn't using the pop correctly.

oracleguy
10-11-2008, 12:17 AM
What is the output of the program that you are getting? I don't see anything wrong with that code.

The reason 0 isn't being outputted is that you need to call DisplaySum once before you start your loop is all.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum