组装 8086 |数组的总和,打印多位数字 [英] Assembly 8086 | Sum of an array, printing multi-digit numbers

查看:25
本文介绍了组装 8086 |数组的总和,打印多位数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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] 实际上是 mov al, [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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆