140 likes | 377 Views
Stack and Subroutines. Module M17.1 Section 11.2. The Stack. A region of memory in the stack segment (SS) used for storing temporary data. The stack is a Last-In-First-Out (LIFO) data structure (like a stack of plates). The stack pointer points to the value on top of the stack.
E N D
Stack and Subroutines Module M17.1 Section 11.2
The Stack • A region of memory in the stack segment (SS) used for storing temporary data. • The stack is a Last-In-First-Out (LIFO) data structure (like a stack of plates). • The stack pointer points to the value on top of the stack. • The stack grows toward lower memory addresses. That is, the stack pointer is decremented before an element is written to memory.
Push and Pop Example 0000 B8 34 12 MOV AX,1234H 0003 BB 78 56 MOV BX,5678H 0006 50 PUSH AX 0007 53 PUSH BX 0008 58 POP AX 0009 5B POP BX
Subroutine Example 0000 E8 05 00 CALL 0008H 0003 E8 0A 00 CALL 0010H 0006 CC INT 3 0008 E8 05 00 CALL 0010H 000B C3 RET 0010 C3 RET Note: Destination address = Displacement + Offset address of next instruction 0005 + 0003 = 0008 000A + 0006 = 0010 0005 + 000B = 0010
SCREEN.ASM Subroutines • deccur • decrement the screen cursor • pbyte • print the hex value of the byte in AL followed by a space • crlf • carriage return - line feed: move cursor to beginning of next line • quit • return to DOS
Quit to DOS quit proc near mov ax,4C00h int 21h quit endp
Stack segment Data segment Subroutines from file SCREEN.ASM
numer 78 56 numer+2 34 12 denom BC 9A
Must always set DS to DATA dx:ax = numer bx = denom quotient in ax remainder in dx
;save registers ;save ax in bx ;print high byte ;decrement cursor ;print low byte ;decrement cursor ;restore registers