我试图从用户那里获取 5 个数字,并将这些数字存储在一个向上增长的堆栈中。然后它根据 LIFO(后进先出)概念显示堆栈的内容。我在 EMU8086 上遇到了 :stack 错误的重复声明。我不知道为什么。
.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
我在 EMU8086 上遇到了 :stack 错误的 重复声明。
.data stack_size equ 5 stack db stack_size dup(?) top db 0
通常
stack
将是汇编器的保留字,并且不允许您像在这里尝试那样将其用作普通标签。也许把名字改成MyStack
?
mov [stack + top], al ; Push the number onto the stack mov al, [stack + top] ; Pop the number from the stack
在这些方括号之间,您写了两个地址的相加,这是错误的。您需要获取顶部的内容(所以不是它的地址)并且您必须将其作为字大小的值获取,以便将其添加到用户定义的“堆栈”的已经字大小的地址中。下一个代码假设您将 top 定义为
top dw 0
。
mov bx, top
mov [MyStack + bx], al ; Push the number onto the stack
inc top ; Move the top pointer up
; Display the number mov ah, 0 add al, '0' ; Convert numeric value to ASCII int 21h
可以显示字符的 DOS 函数的函数号为 02h,并且它的唯一参数位于 DL 寄存器中。您编写的函数编号 00h 将立即终止您的程序!
top dw 0
msg db 'Enter a number: $'
newline db 0Ah, 0Dh, '$'
MyStack_size equ 5
MyStack db stack_size dup(?)
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
; 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