我在 assembly / emu8086 中使用数组时遇到问题
我是组装的初学者。我想打印2个数组成员。
这就是我第一次编写代码的方式:
mov PRIME_NUMBERS[1],2
mov ax,PRIME_NUMBERS[1]
call PRINT
mov PRIME_NUMBERS[2],3
mov ax,PRIME_NUMBERS[2]
call PRINT
它打印了“ 23”,还可以,但是当我像这样编码时:
mov PRIME_NUMBERS[1],2
mov PRIME_NUMBERS[2],3
mov ax,PRIME_NUMBERS[1]
call PRINT
mov ax,PRIME_NUMBERS[2]
call PRINT
它打印“ 7703到”。元素编号1似乎已被数字770所取代。我找不到我的错误!
我的代码:
.MODEL SMALL
.STACK 100H
.DATA
max_num_check dw ?
current_num dw ?
prime_numbers dw 100 DUP (0)
.CODE
MAIN PROC
;define data segment
mov ax,@DATA
mov DS,ax
;------------------
mov PRIME_NUMBERS[1],2
mov ax,PRIME_NUMBERS[1]
call PRINT
mov PRIME_NUMBERS[2],3
mov ax,PRIME_NUMBERS[2]
call PRINT
HLT
MAIN ENDP
; to print numbers , you should use mov ax,number and CALL PRINT
PRINT PROC
mov cx,0
mov dx,0
mov bx,10
l1:
cmp ax,0
je l2
div bx
push dx
inc cx
xor dx,dx
jmp l1
l2:
cmp cx,0
je end
pop dx
add dx,'0'
dec cx
mov ah,2h
int 21h
jmp l2
end:
xor ax,ax
ret
PRINT ENDP
MAX_NUM_TO_CHECK PROC
mov cx,0
here:
mov ax,cx
mul cx
cmp ax,current_num
jge store
inc cx
jmp here
store:
mov max_num_check,cx
ret
MAX_NUM_TO_CHECK ENDP
END MAIN
I am a very beginner in assembly. I want to print 2 members of an array.
This is how I first wrote my code:
mov PRIME_NUMBERS[1],2
mov ax,PRIME_NUMBERS[1]
call PRINT
mov PRIME_NUMBERS[2],3
mov ax,PRIME_NUMBERS[2]
call PRINT
and it prints "23" and it's ok, but when I code like:
mov PRIME_NUMBERS[1],2
mov PRIME_NUMBERS[2],3
mov ax,PRIME_NUMBERS[1]
call PRINT
mov ax,PRIME_NUMBERS[2]
call PRINT
it prints "7703و". The element number 1 seems to have been replaced by the number 770. I can not find my mistake!
my code :
.MODEL SMALL
.STACK 100H
.DATA
max_num_check dw ?
current_num dw ?
prime_numbers dw 100 DUP (0)
.CODE
MAIN PROC
;define data segment
mov ax,@DATA
mov DS,ax
;------------------
mov PRIME_NUMBERS[1],2
mov ax,PRIME_NUMBERS[1]
call PRINT
mov PRIME_NUMBERS[2],3
mov ax,PRIME_NUMBERS[2]
call PRINT
HLT
MAIN ENDP
; to print numbers , you should use mov ax,number and CALL PRINT
PRINT PROC
mov cx,0
mov dx,0
mov bx,10
l1:
cmp ax,0
je l2
div bx
push dx
inc cx
xor dx,dx
jmp l1
l2:
cmp cx,0
je end
pop dx
add dx,'0'
dec cx
mov ah,2h
int 21h
jmp l2
end:
xor ax,ax
ret
PRINT ENDP
MAX_NUM_TO_CHECK PROC
mov cx,0
here:
mov ax,cx
mul cx
cmp ax,current_num
jge store
inc cx
jmp here
store:
mov max_num_check,cx
ret
MAX_NUM_TO_CHECK ENDP
END MAIN
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
770在十六进制中为0302。请注意,值3出现在上字节中。
汇编器不会按数组成员的大小扩展下标,因此Prime_numbers [1]是Prime_numbers+1,而不是Prime_numbers+2,如您所愿。
每个数字占两个字节。它将第一个数字存储到字节prime_numbers+1和prime_numbers+2中。
它将第二个数字存储到Bytes Prime_numbers+2和Prime_numbers+3中,覆盖了第一个数字的上字节。然后,当它试图读取第一个数字时,它将获得第一个数字的一个字节,而第二个字节的一个字节被挤入一个值。
(实际上,您可能想要的是要存储在Prime_numbers+0中的第一个数字,第二个数字存储在Prime_numbers+2中。)
770 is 0302 in hex. Note that the value 3 appears in the upper byte.
The assembler does not scale the subscript by the size of the array member, so PRIME_NUMBERS[1] is PRIME_NUMBERS+1 instead of PRIME_NUMBERS+2, as you intended.
Each number occupies two bytes. It is storing the first number into the bytes PRIME_NUMBERS+1 and PRIME_NUMBERS+2.
It is storing the second number into the bytes PRIME_NUMBERS+2 and PRIME_NUMBERS+3, overwriting the upper byte of the first number. Then when it tries to read the first number, it gets one byte of the first number and one byte of the second number squished into a single value.
(Actually what you probably intended is for the first number to be stored in PRIME_NUMBERS+0 and the second number to be stored in PRIME_NUMBERS+2.)