Stack


Showing how to use stack 
;this program shows how to use stack
;how to push on stack
;how to make local variable, and how to access them
;I am using pop to get any value for printing
;so value will not remain on stack
;go through it step by step and see what happens on stack
;remember stack is LIFO (last in first out)
org 100h
mov ax, '1'
push ax  ; 1st element on stack
mov ax, '2'; 2nd element on stack
push ax

push bp ; save old value of bp
mov bp, sp ; make bp our reference point
sub sp, 2 ; creates a local variable,it can be accessed by [bp-2]

mov ax,'3'
mov [bp],ax

mov ax,'4'
mov [bp-2],ax

mov bx,'5'
push bx ; 5th element on stack
mov cx,'6'
push cx ; 6th element on stack

pop dx ;dx=6
call print
call printdash
pop dx ;dx=5
call print
call printdash
pop dx ;dx=4
call print
call printdash
pop dx ;dx=3
call print
call printdash
pop dx ;dx=2
call print
call printdash
pop dx ;dx=1
call print

MOV AH,4CH
INT 21H

print:
mov ah , 2h
int 21h
ret

printdash:
mov dx,'-'
mov ah,2h
int 21h

ret


Now I will show how to use bp with stack
;this program shows how to use stack
;how to push on stack
;how to make local variable, and how to access them
;I am not using pop to get any value for printing
;so value will remain on stack, so that it can be used again
;go through it step by step and see what happens on stack
org 100h
mov ax, '1'
push ax  ; 1st element on stack
mov ax, '2'
push ax  ; 2nd element on stack

push bp ; save old value of bp
mov bp, sp ; make bp our reference point
sub sp, 2 ; creates a local variable,it can be accessed by [bp-2]

mov ax,'3'
mov [bp],ax

mov ax,'4'
mov [bp-2],ax

mov bx,'5'
push bx ; 5th element on stack
mov cx,'6'
push cx ; 6th element on stack

mov dx, [bp+4] ;dx=1
call print
call printdash
mov dx, [bp+2] ;dx=2
call print
call printdash
mov dx, [bp]   ;dx=3
call print
call printdash
mov dx, [bp-2] ;dx=4
call print
call printdash
mov dx, [bp-4] ;dx=5
call print
call printdash
mov dx, [bp-6] ;dx=6
call print

MOV AH,4CH
INT 21H

print:
mov ah , 2h
int 21h
ret

printdash:
mov dx,'-'
mov ah,2h
int 21h

ret


Converting to uppercase (stack is being used)
;This program prints in upper case, 
;if any character is present in lower case, it will be converted to upper case
;it shows how to use a stack
org 100h 
arr dw 'H','a','P','p','y',' ','&',' ','j','O','Y' ; this array has a sentence
length dw 11 
for_upper dw 32
lower_limit_97 dw 97
upper_limit_122 dw 122
two dw 2

mov bx,20; this is equal to last index of array
mov cx,0 ; counter for stack
jmp start

uppercase: ;converting to upper case
mov si,[for_upper];moving 32 to si register
sub ax,si
ret

print: ;printing 
mov dx ,ax
mov ah , 2h 
int 21h
ret

start:
    mov ax,[arr+bx]
    mov si,[lower_limit_97]
cmp ax,si;checking if character is less than 97
jl ok
    mov si,[upper_limit_122]
cmp ax,si;checking if character is greater than 122
jg ok

call uppercase

ok:
push ax;pushing in stack
inc cx;cx will keep count of numbers in stack

sub bx,[two]
dec [length]
jnz start
loop:;printing after poping elements from array 
pop ax;poping from stack
call print;printing poped character
dec cx
jnz loop
end:
MOV AH,4CH 
INT 21H


No comments:

Post a Comment