I am trying to get 5 numbers from the user and store those numbers in an upward growing stack. Then it displays the contents of the stack based on the LIFO (Last in first out) concept. I'm running into the duplicate declaration of :stack error on EMU8086. I have no idea why.
.model small
.stack 100h
.data
stack_size equ 5
stack db stack_size dup(?)
top db 0
count db 5
msg db 'Enter a number: $'
newline db 0Ah, 0Dh, '$'
.code
mov ax, @data
mov ds, ax
mov cx, stack_size
input_loop:
lea dx, msg
mov ah, 09h
int 21h ; Display the input message
mov ah, 01
int 21h ; Read a character from the user
sub al, '0' ; Convert ASCII to numeric value
mov [stack + top], al ; Push the number onto the stack
inc top ; Move the top pointer up
; Display newline
lea dx, newline
mov ah, 09h
int 21h
loop input_loop
; Output loop to display numbers based on LIFO concept
mov cx, stack_size
output_loop:
dec top ; Move the top pointer down
mov al, [stack + top] ; Pop the number from the stack
; Display the number
mov ah, 0
add al, '0' ; Convert numeric value to ASCII
int 21h
; Display newline
lea dx, newline
mov ah, 09h
int 21h
loop output_loop
mov ah, 4ch
int 21h
end
Normally
stackwould be a reserved word for the assembler, and you would not be allowed to use it as a normal label like you are trying to do here. Perhaps change the name intoMyStack?Now the program assembles, but will it run?
Between these square brackets you have written the addition of two addresses and that is wrong. You need to fetch the contents of top (so not its address) and you must fetch it as a word-sized value in order to add it to the already word-sized address of your user defined 'stack'. Next code assumes that you define top as
top dw 0as well as rename stack into MyStack:The DOS function that can display a character has function number 02h, and it expects its sole argument in the DL register. The function number 00h that you have written will terminate your program immediately!
The data
The input
The output