Pregunta

So this is the assembly code for the program mentioned:

.model small
.386p
.data
    n1 dd 12345678h
    n2 dd 11112222h
    res dd ?
.code
.startup
    mov eax, n1
    mov ebx, n2
    add eax, ebx
    mov res,eax
    call disp

    disp proc near
    mov ch,08h
up:
    mov eax,res
    rol eax, 04h
    mov res,eax
    and al, 0Fh
    cmp al, 0Ah
    JC d1
    add al,07h
    d1: add al,30h

    mov dl, al

    mov ah, 02h
    int 21h
    dec ch
    JNZ up

    ret
    endp disp
    mov ah,4ch
    int 21h

end
.exit

Would someone be kind enough to explain what the procedure "disp" does exactly step wise? The addition part makes sense though the rest eludes me. Thanks!

¿Fue útil?

Solución

First it rotates the 32-bit value in res so that the previous 4 most significant bits end up in the 4 least significant bits:

mov eax,res
rol eax, 04h
mov res,eax

For example, if res held 0x12345678 at the first line, eax and res would both hold 0x23456781 after the third line.

It then masks away all but the 4 least significant bits of al:

and al, 0Fh

If we continue with my example above, al will now contain 0x01.

It then adds 7 to al if al >= 0x0A (JC can be viewed as Jump if unsigned less than), and finally adds '0' (0x30) to al:

cmp al, 0Ah
JC d1
add al,07h
d1: add al,30h

If al originally was in the range 0..9 it will now be in the range 0x30..0x39 (ASCII '0'..'9'). If it was originally in the range 0x0A..0x0F it will now be in the range 0x41..0x46 (ASCII 'A'..'F').

Finally it prints the resulting character using int 21h / ah=02h (WRITE CHARACTER TO STANDARD OUTPUT).

It then repeats this 7 more times (i.e. for every hex digit in the 32-bit input number).

Otros consejos

First of all: The sequence

mov ah,4ch
int 21h

(exit program) is at the wrong place. It must be right before the disp proc near.

The procedure disp displays the unsigned integer in res in hexadecimal notation:

disp proc near
    mov ch,08h          ; loop 8 times 'up'
up:
    mov eax,res         ; load value in 'result'
    rol eax, 04h        ; move the highest nibble to the lowest place
    mov res,eax         ; save eax for future use (next pass of 'up')
    and al, 0Fh         ; isolate the nibble
    cmp al, 0Ah         ; al < 0A?
    JC d1               ; yes: skip the next command
    add al,07h          ; add 7 to match the appropriate character ('A' - 'F')
    d1: add al,30h      ; convert AL to ASCII
    mov dl, al
    mov ah, 02h         ; Fn 02h: Write character in DL to Standard Output (Stdout)
    int 21h             ; call MSDOS-interrupt
    dec ch              ; loop 8 times
    JNZ up
    ret
endp disp               ; TASM. For MASM use 'disp endp'
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top