x86 assembly (masm32) - how to split multi-digit d

2019-08-25 03:55发布

问题:

I am still getting my head around x86 assembly, and so I have made this little program that multiplies 6 and 7, moves the data to EAX and then prints the result. It compiles fine, and runs fine, but instead of printing 42, it prints the fourty-second ASCII character. I have on this forum how to print a single-character number, but now I need to figure out how to print multi-digit numbers. Here is my code:

.386
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib
.data
    adrs dd 0
.code
start:
    mov eax, 6
    mov ebx, 7
    imul eax, ebx
    add eax, 48
    mov adrs, eax
    invoke StdOut, addr adrs
    invoke ExitProcess, 0
end start

So in summary, I need to understand how to split data into individual characters, so that I can print them. Any help would be great.

Regards,

Progrmr

回答1:

Divide your number by 10 repeatedly. Collect remainders. Add to them ASCII code of '0', print.



回答2:

Here is a code snippet which takes a number (var sum) and finds how many hundreds, tens and units are in that sum by dividing the sum by 100, 10 and remainder is units. It stores all these values in ARRAY after adding 30H to it. Now ARRAY is the ASCII equivalent of the number in sum.

 :
 ARRAY   Db  4 dup(?),0
 sum DW  253D
 :
     mov esi, offset ARRAY
     mov ax, word ptr sum2
     mov bl,100D
     div bl      ; ah - R and al - Q    
     mov bh, ah    
     add al,30h
     mov [esi], al
     add esi,1

     mov ah,00
     mov al,bh
     mov bl,10D
     div bl
     mov bh, ah
     add al,30h
     mov [esi], al
     add esi,1

     mov dl,bh
     add dl,30h
     mov [esi],dl

     lea dx,offset RESULT2
     mov ah,09
     int 21h
     mov esi, offset ARRAY
     mov cl,04
loopdisplay1:
     mov dl,[esi]
     mov dh,00
     mov ah,02
     int 21h
     add esi,1
     dec cl
     jnz loopdisplay1