TheInsider
Programmer
Hello,
I'm using ArrowSoft's 8086 Assembly compiler/linker. To avoid pasting oodles of code, I have written a simplified version of the code, below:
In summary, I have 3 PROCs. Each procedure uses CX to LOOP. As you can see, this requires that CX be stored [on the stack] before calling each procedure -- in order to ensure that CX will ultimately retain the correct value within each procedure.
At the same time I am trying to pass parameters by pushing them on the stack and popping them off the stack. I also wish to place return values from procedures onto the stack, rather than passing and returning via registers.
The problem is that, although I am pushing and popping in the correct order, CALL and RET are placing the IP on the stack and it is causing chaos! The program crashes because the wrong values are getting pushed and popped.
How can I modify this so that I can save CX each time, pass parameters via the stack, and return values via the stack?
Thanks
I'm using ArrowSoft's 8086 Assembly compiler/linker. To avoid pasting oodles of code, I have written a simplified version of the code, below:
Code:
main proc
... ;do something
call function1
... ;do something
main endp
function1 proc
mov cx, 0Ah ;setup counter for loop of 10
label1:
push cx ;function2 uses cx as well, so save current value onto stack
mov bx, 01h ;setup argument for function 2
push bx ;push argument onto stack
call function2
pop cx ;restore original value of cx from stack, so counter is correct
loop label1
ret
function1 endp
;------------------------------------------------------------------------------------------------
function2 proc
pop bx ;pop parameter value off of the stack
mov cx, 05h ;setup counter for loop of 5
label2:
push cx ;function3 uses cx as well, so save current value onto stack
... ;do something with bx
mov cx, 0FFh ;setup argument for function 3
push cx ;push argument onto stack
call function3
pop cx ;function3 placed a return value in cx
... ;do something with cx
pop cx ;restore original value of cx from stack, so counter is correct
loop label2
ret
function2 endp
;------------------------------------------------------------------------------------------------
function3 proc
pop cx ;pop parameter value off of the stack (used for loop counter)
label3:
... ;do something
loop label3 ;loop as many times as parameter value requested
... ;do something with cx
push cx ;push value of cx onto stack for return value
ret
function3 endp
In summary, I have 3 PROCs. Each procedure uses CX to LOOP. As you can see, this requires that CX be stored [on the stack] before calling each procedure -- in order to ensure that CX will ultimately retain the correct value within each procedure.
At the same time I am trying to pass parameters by pushing them on the stack and popping them off the stack. I also wish to place return values from procedures onto the stack, rather than passing and returning via registers.
The problem is that, although I am pushing and popping in the correct order, CALL and RET are placing the IP on the stack and it is causing chaos! The program crashes because the wrong values are getting pushed and popped.
How can I modify this so that I can save CX each time, pass parameters via the stack, and return values via the stack?
Thanks