Is this code correct (Number plus number, then print the result)

前端 未结 1 1657
心在旅途
心在旅途 2020-11-30 16:11

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

I did that code:

.         


        
相关标签:
1条回答
  • 2020-11-30 16:19

    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.

    0 讨论(0)
提交回复
热议问题