PUSH Push data onto stack - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

PUSH Push data onto stack

Description:

Pop restores the register contents from the stack memory. ... MAKE SURE THAT PUSH AND POP INSTRUCTIONS WITHIN A PROCEDURE ARE BALANCED. ... – PowerPoint PPT presentation

Number of Views:129
Avg rating:3.0/5.0
Slides: 10
Provided by: drmar
Category:
Tags: push | data | onto | pop | push | stack

less

Transcript and Presenter's Notes

Title: PUSH Push data onto stack


1
PUSH (Push data onto stack)
PUSH 16-bit register SP ? SP-1, SSSP ?
high byte SP ? SP-1, SSSP
? low byte
Push stores the register contents in the stack
memory. The stack pointer is decremented so that
it points to the last byte stored .
MOV AX, 0abcdh initialize AX current
content of SP is 0fffeh PUSH AX after
execution the status is 0fffdh ? 0abh,
0ffch ? 0cdh SP ? 0fffch
2
POP (Pop data from stack)
POP 16-bit register low byte of register
? SSSP, SP ? SP 1, high byte of register
? SSSP, SP ? SP 1
Pop restores the register contents from the stack
memory. The stack pointer is incremented so that
it points to the low byte of the last register
Pushed on to the stack .
MOV AX, 0ffffh initialize AX current
content of SP is 0fffch, 0fffc holds 0cdh,
offfd holds 0abh POP AX after execution the
status is AX ? 0abcdh SP ? 0fffeh
3
PUSH and POP extensions
The A86 assembler supports the following
extensions to the PUSH and POP instructions ?
Push and Pop may have multiple arguments ? PUSH
AX, BX, CX is assembled as PUSH AX PUSH BX PUSH CX
Restoring registers PUSH AX, BX, CX is
restored by POP CX, BX, AX
4
CALL (Call a procedure)
CALL destination SSSP ? address of the
instruction following Call IP ? destination,
SP ? SP - 2
Procedure is the Intel name for a subroutine.
Call transfers execution to the destination and
stores the return address on the stack
Address Instruction 0100 Call Myproc this
takes 3 bytes -- 1 for the opcode and 2
bytes for the address of Myproc 0103 next
instruction after execution, the IP is set to
the address of the first instruction of Myproc
the last data on the stack are 0103 (the address
of the next instruction) and the stack pointer
has been decremented by 2
5
RET (Return from a procedure)
RET IP ? SS SP SP ? SP 2
Return loads the return address into the IP from
the stack. The stack pointer is incremented by 2
The RET instruction takes no arguments. The
current contents of the stack are loaded into the
IP. Therefore, the programmer must be very
careful that the data stored there is the return
address. This means that any data PUSHed onto
the stack by the procedure must be POPed off the
stack before RET.
6
Housekeeping
It is good practice for a procedure to do
housekeeping so that the procedure changes only
the registers it is meant to change, and that the
main program can expect that the other registers
are left unchanged.
For example, consider a procedure that is
expected to return the answer in the BX register,
but uses the AX and CX registers in its
algorithm. The procedure should follow this
model Myproc the label denoting this
procedure PUSH AX, CX save registers on
stack blah, blah, blah the algorithm MOV BX,
result store the answer in BX POP CX, AX
restore registers RET
7
Crashing the computer
Because RET takes the return address of the
stack, a RET instruction where the data on the
stack is not a return address is likely to crash
the computer, requiring a reboot. To avoid
this MAKE SURE THAT PUSH AND POP INSTRUCTIONS
WITHIN A PROCEDURE ARE BALANCED. That is, make
sure that any register PUSHed on to the stack is
POPed off the stack before the RET instruction.
8
Debugging tips
F1 is single-step F2 is procedure step--an entire
procedure is executed and the debug mode is
re-entered after the RET instruction F9 is the
go until here instruction--it is useful for
executing a single time through a loop to
examine the effect of the loop without
single-stepping through every instruction G,
address is the go until you reach address
instruction. It is useful for executing a large
block of code that has already been debugged but
is necessary to set the status for the code
that follows
9
Time-saving tips
I find that 3 simple batch files save lots of
typing (and mis-typing and frustration). At the
beginning of a project I create 3 simple files
p.bat, a.bat, and d.bat. Their contents are
shown below p.bat ped filename.ext a.bat a86
filename.ext d.bat d86 filename Using
these files, you can repeat the
edit/assemble/debug process with just 3
keystrokes.
Write a Comment
User Comments (0)
About PowerShow.com