How to fix duplicate declaration of stack in x86

36 views Asked by At

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
1

There are 1 answers

0
Sep Roland On

I'm running into the duplicate declaration of :stack error on EMU8086.

.data
    stack_size equ 5
    stack db stack_size dup(?)
    top db 0

Normally stack would 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 into MyStack ?

Now the program assembles, but will it run?

  1. mov [stack + top], al ; Push the number onto the stack
    mov al, [stack + top] ; Pop the number from the stack
    

    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 0 as well as rename stack into MyStack:

    mov  bx, top
    mov  [MyStack + bx], al ; Push the number onto the stack
    inc  top                ; Move the top pointer up
    
  2. ; Display the number
    mov ah, 0
    add al, '0' ; Convert numeric value to ASCII
    int 21h
    

    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

    top          dw 0
    msg          db 'Enter a number: $'
    newline      db 0Ah, 0Dh, '$'
    MyStack_size equ 5
    MyStack      db MyStack_size dup(?)

The input

    mov  bx, top
    mov  cx, MyStack_size
input_loop:
    mov  dx, offset msg
    mov  ah, 09h
    int  21h

    mov  ah, 01h
    int  21h
    sub  al, '0'          ; Convert ASCII to numeric value
    mov  [MyStack + bx], al ; Push the number onto the stack
    inc  bx               ; Move the top pointer up

    mov  dx, offset newline
    mov  ah, 09h
    int  21h

    loop input_loop
    mov  top, bx

The output

    ; Output loop to display numbers based on LIFO concept
    mov  bx, top
    mov  cx, MyStack_size
output_loop:
    dec  bx               ; Move the top pointer down
    mov  dl, [MyStack + bx] ; Pop the number from the stack

    add  dl, '0'          ; Convert numeric value to ASCII
    mov  ah, 02h
    int  21h

    mov  dx, offset newline
    mov  ah, 09h
    int  21h

    loop output_loop