这段代码是否正确(数字加数字,然后打印结果) [英] Is this code correct (Number plus number, then print the result)

查看:16
本文介绍了这段代码是否正确(数字加数字,然后打印结果)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to do something simple in assembly language.
addition two numbers, and print the result on the screen.

我做了那个代码:

.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

代码编译无误后,告诉我一个奇怪的字符.

After compile the code without any error, tells me a strange character .

修改:

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

MOV ah,02h
INT 21h 

但仍然打印一个奇怪的字符我不知道我该怎么做才能在屏幕上打印数字

but still print a strange character I don't know what can I do to print number on the screen

推荐答案

是的,您很可能会得到一个奇怪的字符,因为 int 21/ah=02 要求打印的字符位于dl 注册,并且您还没有向 dl 填充任何东西.

Yes, you will most likely get a strange character because int 21/ah=02 requires the character to print to be in the dlregister, and you haven't populated dl with anything.

您可能希望通过以下方式传输值:

You may want to transfer the value with something like:

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 中断会为它们输出什么.

However, keep in mind that, even if you do transfer the value from al to dl, character number 15 may not be what you expect. 15 is one of the ASCII control characters and I'm not sure what the DOS interrupts will output for them.

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

If you want to print out the digits15, you will need two calls, one with dl = 31h and the second with dl = 35h (the two ASCII codes for the 1 and 5 characters).

如果您想知道如何在寄存器中取一个数字并以可读形式输出该数字的数字我之前的回答.

If you want to know how to take a number in a register and output the digits for that number in readable form, there's some pseudo-code in an earlier answer of mine.

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

From that answer, you have the pseudo-code:

    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 中所需的值开始:

Now you need to translate that into x86 assembly. Let's start with the desired value in 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 只留下十位和个位数字.

Now that code segment (notwithstanding any errors due to the fact it's been a while since I did assembly) should print out the hundreds digit and leave ax with only the tens and units digit.

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

It should be a simple matter to duplicate the functionality twice more to get the tens and units places.

这篇关于这段代码是否正确(数字加数字,然后打印结果)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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