在汇编语言中加入2号和打印结果 [英] add 2 numbers in assembly language and print the result

查看:682
本文介绍了在汇编语言中加入2号和打印结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要帮助,如何添加两个数字,然后在这里打的结果是我的code

need help, how to add two numbers and then print the result here is my code

    .MODEL SMALL
    .STACK 200H
    .DATA
NUM1 DB 12
NUM2 DB 3
VAL  DB ?
MSG1 DB "The sum is : $"

    .CODE
BEGIN PROC 
      MOV AX, @DATA
      MOV DS, AX

      MOV AL, NUM1
      ADD AL, NUM2
      MOV VAL, AL



      LEA DX, MSG1
      MOV AH, 9
      INT 21H


      MOV AH, 2
      MOV DL, VAL
      INT 21H

      MOV AX, 4C00H
      INT 21H
BEGIN ENDP
      END BEGIN

我得到的输出,上面写着

I got an output that says

The sum is 0 

什么是错误我的code?

What is the error to my code?

推荐答案

通过不断分度值10,你会得到的余数的个位数 - 但在错误命令(后到前)。要通过 PUSH 荷兰国际集团和 POP ING打印在正确的顺序(先最后一个),你可以扭转他们(关键字:先出后进先出法=最后一个):

By dividing the value constantly by 10 you'll get the single digits in the remainder - but in the "wrong" order (last to first). To print it in the "right" order (first to last) you can reverse them by PUSHing and POPing (keyword: LIFO = last in first out):

.MODEL SMALL
.STACK 200H
.DATA
    NUM1 DB 12
    NUM2 DB 3
    VAL  DW ?
    MSG1 DB "The sum is : "
    DECIMAL  DB "00000$"

.CODE
BEGIN PROC
    MOV AX, @DATA
    MOV DS, AX

    XOR AX, AX
    MOV AL, NUM1
    ADD AL, NUM2
    ADC AH, 0
    MOV VAL, AX

    MOV AX, VAL
    CALL AX_to_DEC

    LEA DX, MSG1
    MOV AH, 9
    INT 21H

    MOV AX, 4C00H
    INT 21H
BEGIN ENDP

AX_to_DEC PROC
        mov bx, 10              ; divisor
        xor cx, cx              ; CX=0 (number of digits)

    First_Loop:
        xor dx, dx              ; Attention: DIV applies also DX!
        div bx                  ; DX:AX / BX = AX remainder: DX
        push dx                 ; LIFO
        inc cx                  ; increment number of digits
        test  ax, ax            ; AX = 0?
        jnz First_Loop          ; no: once more

        mov di, OFFSET DECIMAL  ; target string DECIMAL
    Second_Loop:
        pop ax                  ; get back pushed digit
        or ax, 00110000b        ; to ASCII
        mov byte ptr [di], al   ; save AL
        inc di                  ; DI points to next character in string DECIMAL
        loop Second_Loop        ; until there are no digits left

        mov byte ptr [di], '$'  ; End-of-string delimiter for INT 21 / FN 09h
        ret
AX_to_DEC ENDP

END BEGIN

这篇关于在汇编语言中加入2号和打印结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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