首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我做了一个计算的循环,现在我想在汇编中对结果求和。

我做了一个计算的循环,现在我想在汇编中对结果求和。
EN

Stack Overflow用户
提问于 2019-10-25 07:16:42
回答 1查看 52关注 0票数 2

我们的任务是从用户那里获取5个输入

输入的数字应该从9中减去,然后我们应该对所有的总差值求和。

例如输入5, 2,4,7,1 9-5= 4,9-2=7,9-4=5,9-7= 2,9-1= 8,然后我们需要添加结果,因此4+7+5+2+8= 26

并显示以下结果

我已经进行了计算,并将它们放在一个循环中,但总和只显示上次计算的值,而不是总和。

代码语言:javascript
复制
.model small
.stack 100h
.data

 num1 db
 num2 db 9
 result db ?
 sum db

 msg1 db 10, 13,"Enter a number: $", 10, 13
 msg2 db 10, 13,"Difference is: $",10, 13
 msg3 db 10, 13,"Sum is: $",10, 13

 .code
     main proc

     mov ax, @data
        mov ds, ax

        mov cx,5

        process:

        mov dx, offset msg1
        mov ah,9 ; used to print the string/msg with
        int 21h ;this



        mov ah, 1 ;READ a Character from Console,
                  ;Echo it on screen and save the
                  ;value entered in AL register
        int 21h

        sub al, 30h ;keep the value enter in bcd form
        mov num1, al ;move num1 to al
        ;al is used for input


        ;sub al, 30h
        mov al, num2 ;move num2 to al

        sub al, num1 ;

        mov result, al ;move whats in al to result


        mov bl,al
        add sum,bl
        ;add al,sum

        mov ah,0 ;clears whats in ah
        aaa ;used to convert the result to bcd
        ;and first digit is stored in ah
        ;second digit stored in al


        add ah, 30h ;convert whats in ah to ascii by adding 30h
        add al, 30h ;convert whats in al to ascii by adding 30h
        mov bx, ax ;saving whats in ah and al in bx register



        mov dx, offset msg2
        mov ah,9 ; used to print the string/msg  
        int 21h



        ;the following is used to print whats in the bh register
        ;dl is used for output
        ;2 or 2h means to write/print whats in dl
        ;so the value to be printed is moved to dl
        mov ah,2
        mov dl, bh
        int 21h


        ;the following is used to print whats in bl
        mov ah,2
        mov dl, bl
        int 21h

        loop process

        mov dx, offset msg3
        mov ah,9 ; used to print the string/msg  
        int 21h

        mov ah,2
        mov dl, bh
        int 21h


        ;the following is used to print whats in bl
        mov ah,2
        mov dl, bl
        int 21h

     mov ah, 4ch ;4ch means to return to OS i.e. the end
     ;of program
     int 21h
    main endp ;ends the code
end main ;ends main

我预计我的总和是26。相反,我得到了8

EN

回答 1

Stack Overflow用户

发布于 2019-10-26 18:53:23

sum数据库

这缺少一个操作数。写入sum db 0

,但sum仅显示上次计算的值,而不显示sum

在显示第三条消息和相应的值之间,您忘记了重新计算所需的BX内容。你只是简单地重用了里面已经存在的东西!

代码语言:javascript
复制
    mov dx, offset msg3
    mov ah, 09h
    int 21h

    mov al, sum         ADD THIS
    mov ah, 0           ADD THIS
    aaa                 ADD THIS
    add ax, 3030h       ADD THIS
    mov bx, ax          ADD THIS

    mov ah, 02h
    mov dl, bh
    int 21h

    mov ah, 02h
    mov dl, bl
    int 21h

    mov ax, 4C00h
    int 21h
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58550304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档