宏打印存储在字节中的值.装配体 [英] Macro printing a value stored in a byte. Assembly masm
问题描述
组装,masm
嘿,我写了宏,该宏打印出存储在dane1段中的1字节值.
Hey, I wrote macro which prints the 1 byte value stored in dane1 segment.
我将值除以16,然后将提醒推送到堆栈,直到value == 0.然后弹出提醒,将其转换为ASCII代码,然后打印.
I divide the value by 16, then i push reminder to stack until value==0. Then I pop reminders convert them to ASCII code, and print them.
有人可以看看我的代码吗?我应该进行哪些更改以使其更简单?
Shall someone look at my code? What should I change to make it simpler?
我不想每次都做:
mov dl,67h
mov ds:[v1],dl
print v1
是否可以修改此宏,以便它可以使用它:
Is it possible to modify this macro, so that it could use it:
print 67h
print al
感谢您的帮助.
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
推荐答案
使用更新的MASM(版本> = 6),可以使用TYPE
指令为8位寄存器设置特殊条件.另外,看看我的改进之处:
With a newer MASM (version >= 6) you can use the TYPE
directive to make a special condition for 8-bit registers. Also, look at 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
这篇关于宏打印存储在字节中的值.装配体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!