A macro shot of the value stored in a byte. Mom's meeting

Assembly, masm

Hey, I wrote a macro that prints the 1 byte value stored in the dane1 segment.

I divide the value by 16, then I push the reminder on the stack until the value == 0. Then I pop the reminders convert them to ASCII and print them.

Anyone take a look at my code? What should I change to make it easier?

I don't want to do every time:

mov dl,67h
mov ds:[v1],dl
print v1

      

Is it possible to modify this macro so that it can use it:

print 67h
print al

      

Thanks for any help.

assume  cs:code1
.186

dane1   segment
v1 dw 0D9h    ;to use macro
dane1   ends

print macro value
    pusha
    mov ax,seg value
    mov ds,ax
    mov ax,ds:[value]
    xor cx,cx        ;CX - repetitions
    analyse:         ;pushes the digits into stack
        mov dl,16    ;divisor
        div dl                 ;divide number in AX by 16   
        xor dx,dx
        mov  dl,ah ;remainder into the stack   
        push dx 
        xor ah,ah ;        prepare quotient for next loop       
        inc cx               ;increase repetitions    
        cmp ax,0             ;break condition
        jne analyse    
    begin1:                 ;print character stored in the stack
        pop dx              ;pop to DL
        cmp dl,10d          ;is DL digit or char
        jb digit                         
            char:            ;convert to ASCII 
                add dl,55
                jmp begin2  
            digit:
                add dl,'0'
                jmp begin2  
         begin2:
         mov ah,2           ;print character converted to ASCII 
         int 21h
         loop begin1
    popa
endm 

code1   segment

start1: mov ax,seg top1
    mov ss,ax
    mov sp,offset top1 

    print v1

mov ah,4ch
int 21h

code1   ends



stos1   segment stack
    dw  200 dup(?)
top1    dw  ?
stos1   ends

end start1      

      

+3


source to share


1 answer


With the new MASM (version> = 6) you can use a directive TYPE

to create a special condition for 8-bit registers. Also, check out my improvements:



.MODEL small
.386

.STACK
.DATA

.CODE

print_num MACRO value
LOCAL analyse, write, show
    pusha

    IF TYPE(value) eq 1         ; 8-bit register
        movzx ax, value
    ELSE
        mov ax, value
    ENDIF

    xor cx, cx          ; repetitions
    mov bx, 16          ; divisor

    analyse:            ; First step: push the digits into stack
        xor dx, dx      ; Clear DX for division
        div bx          ; DX:AX/BX = AX remainder DX
        push dx
        inc cx
        cmp ax, 0       ; break condition
        jne analyse

    write:              ; Second step: pop the digits from stack and print
        pop dx          ; Only DL is needed
        add dl, "0"
        cmp dl, "9"     ; Is DL digit?
        jbe SHORT show  ; Yes: skip the next line
        add dl, 7       ; Adjust ASCII
        show:
        mov ah, 2       ; Print character to STDOUT
        int 21h
        loop write

    popa
ENDM

Linefeed MACRO
    pusha
    mov ah, 2
    mov dl, 0Dh          ; CR = carriage return
    int 21h
    mov dl, 0Ah          ; LF = line feed
    int 21h
    popa
ENDM

main PROC
    mov ax, @data
    mov ds, ax

    mov ax, 1234h
    print_num ax
    Linefeed

    print_num al
    Linefeed

    print_num 126

    mov ax, 4C00h
    int 21h
main ENDP

END main

      

+2


source







All Articles