组装8086 |数组的总和,输出多位数字 [英] Assembly 8086 | Sum of an array, printing multi-digit numbers
问题描述
我已经在asm x8086中编写了一个非常简单的代码,但遇到了错误.如果有人能为我提供简短的解释,我将不胜感激.
I've written a pretty simple code in asm x8086 and I'm facing an error. If anyone could help me with a brief explanation I would greatly appreciate it.
IDEAL
MODEL small
STACK 100h
DATASEG
; --------------------------
array db 10h, 04h, 04h, 04h, 04h, 04h, 04h, 04h, 04h, 04h
sum db 0
; --------------------------
CODESEG
start:
mov ax, @data
mov ds, ax
; --------------------------
xor cx, cx
mov al, 0
mov bx, offset array
StartLoop:
cmp cx, 10
jge EndLoop
add al, [bx]
add [sum],al
inc cx
inc bx
jmp StartLoop
EndLoop:
mov ah, 09h
int 21h
; --------------------------
exit:
mov ax, 4c00h
int 21h
END start
推荐答案
如您的注释中所述,将add
的更正内容替换为mov
(请注意,以下行:add al,[ bx]实际上是动态的,[bx] )在标签 EndLoop 上只有函数调用是错误的!
With the correction for the add
to be replaced by mov
as noted in your comment (Note that the line: add al, [bx] is actually mov al, [bx]) there's just the function call at the label EndLoop that's wrong!
您要显示总和,并且正在使用DOS打印功能.此功能09h期望您没有提供DS:DX中的指针!
即使您这样做了,您仍然必须将 sum 数字转换为其文本表示形式.
You want to display the sum, and are using the DOS print function. This function 09h expects a pointer in DS:DX that you are not providing!
Even if you did, you would still have to convert the sum number in its text representation.
这里的一种快速解决方案是让自己满意,并仅以单个ASCII字符的形式显示结果.硬编码的总和为52,因此它是一个可显示的字符:
A quick solution here would be to content yourself and just display the result in the form of a single ASCII character. The hardcoded sum is 52 and so it is a displayable character:
EndLoop:
mov dl, [sum]
mov ah, 02h ;Single character output
int 21h
; --------------------------
exit:
mov ax, 4c00h
int 21h
再迈一步,我们可以显示"52":
One step further and we can display "52":
mov al,[sum]
mov ah,0
mov dl,10
div dl ---> AL=5 AH=2
add ax,3030h ---> AL="5" AH="2"
mov dh,ah ;preserve AH
mov dl,al
mov ah,02h
int 21h
mov dl,dh ;restore
int 21h
这篇关于组装8086 |数组的总和,输出多位数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!