【问题标题】:Is this code correct (Number plus number, then print the result)此代码是否正确(数字加数字,然后打印结果)
【发布时间】:2012-04-02 11:49:01
【问题描述】:

我想用汇编语言做一些简单的事情。
将两个数字相加,并在屏幕上打印结果。

我做了那个代码:

.Model SMALL
.Stack 100h

.Code
start:
   MOV ax, 10
   ADD ax, 5
   MOV ah, 02h
   INT 21h 

   MOV ah, 01h
   INT 21h

   MOV ah, 4ch
   INT 21h

end start

编译代码没有任何错误后,告诉我一个奇怪的字符。


修改:

MOV dl, 10
ADD al,5
MOV dl, al

MOV ah,02h
INT 21h 

但仍然打印一个奇怪的字符 我不知道如何在屏幕上打印数字

【问题讨论】:

    标签: assembly x86 dos


    【解决方案1】:

    是的,您很可能会得到一个奇怪的字符,因为 int 21/ah=02 要求要打印的字符在 dlregister 中,而您尚未填充 dl什么都有。

    您可能希望通过以下方式转移价值:

    mov  ax, 10
    add  ax, 5
    
    push ax             ; these are the two new lines.
    pop  dx
    
    mov  ah, 02h
    

    但是,请记住,即使您确实将值从 al 转移到 dl,第 15 个字符也可能不是您所期望的。 15 是 ASCII 控制字符之一,我不确定 DOS 中断会为它们输出什么。

    如果您想打印出数字15,您将需要两次调用,一次调用dl = 31h,第二次调用dl = 35h1 的两个 ASCII 码和5 个字符)。

    如果您想知道如何在寄存器中获取一个数字并以可读形式输出该数字的数字an earlier answer of mine 中有一些伪代码。

    从那个答案,你有伪代码:

        val = 247
    
        units = val
        tens = 0
        hundreds = 0
    loop1:
        if units < 100 goto loop2
        units = units - 100
        hundreds = hundreds + 1
        goto loop1
    loop2:
        if units < 10 goto done
        units = units - 10
        tens = tens + 1
        goto loop2
    done:
        if hundreds > 0:                 # Don't print leading zeroes.
            output hundreds
        if hundreds > 0 or tens > 0:
            output tens
        output units
        ;; hundreds = 2, tens = 4, units = 7.
    

    现在您需要将其转换为 x86 程序集。让我们从ax 中的所需值开始:

        mov  ax, 247                 ; or whatever (must be < 1000)
        push ax                      ; save it
        push dx                      ; save dx since we use it
    
        mov  dx, 0                   ; count of hundreds
    loop1:
        cmp  ax, 100                 ; loop until no more hundreds
        jl   fin1a
        inc  dx
        sub  ax, 100
        jmp  loop1
    fin1a:
        add  dx, 30h                 ; convert to character in dl
        push ax                      ; save
        mov  ah, 2
        int  21h                     ; print character
        pop  ax                      ; restore value
    
        ; now do tens and units the same way.
    
        pop dx                       ; restore registers
        pop ax
    

    现在该代码段(尽管由于我进行汇编已经有一段时间了,因此出现了任何错误)应该打印出百位数字并只留下十位和个位的 ax。

    将功能复制两次以获得十位和个位应该是一件简单的事情。

    【讨论】:

    • 很遗憾,应用您的代码后问题仍然存在。
    • @Lion,请参阅以“但是,请记住...”开头的段落。 什么你真的想打印?如果是 string 15,我添加了最后一段告诉你需要做什么。
    • 没有。我已经给了你足够多的东西,只要你自己付出一点努力,就能实现你想要的。 SO 不是“给我代码”网站。如果你想让一些走狗为你写代码,我建议你去看看 elance 或 Rentacoder 并付费。
    • 首先我感谢你继续帮助我,我并不是说我想要一个走狗来写我的代码,但我不明白任何信息,除非只有例子
    • @Lion:好的,第一件事。 你想输出什么?你还没有告诉我们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    • 2021-03-08
    • 1970-01-01
    • 2020-02-25
    • 1970-01-01
    相关资源
    最近更新 更多