LAB6
LAB6
LAB#6
Objective:
Explain the use of the stack in assembly language
Stack is one dimensional data structure. Items are added and removed from one end of the
structure; that is, it is processed in a “last in first out” manner. The most recent addition to the
stack is called the top of the stack.
PUSH
To add a new word to stack we PUSH it on. The syntax is
PUSH source
POP
To remove the top item from the stack, we POP it. The syntax is
POP destination
Where destination is a 16-bit register (except IP) or memory word. For example
POP DX
Example: Write a program that read a sequence of characters and display them in reverse order
on the next line.
.model small
.stack 100h
.code
main proc
;display user prompt
mov ah,02h
mov dl,'?'
int 21h
;initialize chaaracter count
xor cx,cx
;read a character
mov ah,01h
int 21h
;while character is not a carriage return do
while_:
cmp al,0dh
je end_while
23
Microprocessor & Microcontroller (CE-308)
end_while:
; go to new line
mov ah,02h
mov dl,0dh
int 21h
mov ah,02h
mov dl,0ah
int 21h
jcxz exit ; exit if no character reads
exit:
mov ah,4ch
int 21h
main endp
end main
LAB TASK:
1) Write a program that takes sequence of characters of your ‘name’ as input and display it
in reverse order in new line.
2) Write a program that takes sequence of characters as input and display it in reverse order
in new line. Output will be:
User Input: SSUET
User Input in reverse order: TEUSS
24