Search code examples
assemblyx86reverse-engineeringdos

Explaining some ASM


I've got this ASM code and i need help explaing it, mainly the macro. I've tried a ASM to C disassembly tool, but couldn't get it to work with multiple files and the others were far above my budget.
macro.inc:

pokazvane_cifra_dl macro

push ax
push dx

ad dl,30h
mov ah, 02h
int 21h

pop dx
pop ax
endm

exit macro

mov ah,4ch
int 21h
endm

pokazvane_znak_dl macro nomer_znak

push ax
push dx
mov dl, nomer_znak
mov ah,02h
int 21h
pop dx
pop ax

endm

eho_al macro
push ax
mov ah, 02h
int 21h
pop ax

endm

program.asm:

include macro.inc

.model small .stack 100h .data .code start: mov cx,5 povtori1: mov ah,01h int 21h mov ah,0h push ax loop povtori1 pokazvane_znak_dl 10d pokazvane_znak_dl 13d

mov cx, 5 povtori2: pop dx mov ah, 02h int 21h loop povtori2 exit end start

Any and all help will be appreciated.


Solution

  • pokazvane_cifra_dl macro: displays value dl+48 as ASCII character.

    If dl is value from 0 to 9, it will show the according ASCII digit '0'-'9', so that the reason why the name of macro is something like "display digit" (although you may also call it with for example dl = 40 and it will display ASCII char 'X').


    exit macro: returns control back to DOS (the whole source is DOS-platform targetted, ie. 16 bit real mode x86 assembly with int 21h used for system services, ie needs DOS-like operating system to work).


    pokazvane_znak_dl macro: is slight variation on the first one, this time displaying any ASCII character, like pokazvane_znak_dl 'X' to display 'X'.


    eho_al macro: does display ASCII character from dl. Not sure why the name says eho_al, while it will not use al at all, instead it would destroy the value in al, if it wouldn't do push/pop ax around the int 21h.


    The code itself:

    • will read 5 characters (ASCII) from input, and push them on the stack

    • display two characters 10 and 13 to create "new line" (the proper sequence in DOS is 13 10, the other way).

    • then it will pop those 5 chars one by one back from stack, and display each on screen.

    • exits to DOS.

    I didn't verify there's no bug in the code, but if it is bug free, then the output should look like:

    $prompt> exe.exe
        abcde
        edcba$prompt>
    

    I'm not sure where the DOS prompt will land after execution, whether DOS will insert additional new line, or it will land as I imagined it above. The first "abcde" is input from user, the second is display done by the code.


    EDIT: I forgot that push+pop in loops for input/output will effectively reverse the input "string", as stack is LIFO (Last In First Out) type of queue/container (I still didn't bother to really compile it and run it, so in case you really need 100% answer what the code does, run it).