Is this code correct (Number plus number, then pri

2019-01-09 19:24发布

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

I did that code:

.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 .


Modified:

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

标签: assembly x86 dos
1条回答
聊天终结者
2楼-- · 2019-01-09 20:10

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

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.

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.

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

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.

查看更多
登录 后发表回答