include 'emu8086.inc'
#make_com#
org 100h
s1 dw 50,60,70,80,90,100,120,130,140,160,170,190,190,220,250,270,300
MOV SI,0
MOV CX,16
s2:
MOV AX,s1[SI]
s3:
INC SI
CMP AX,100
JBE s4
JA s5
s4:
PRINTf AX
JMP s3
s5:
CMP AX,200
JB s6
JA s7
s6:
PRINTf AX
JMP s3
s7:
PRINTf AX
JMP s3
END
;printf AX doesn't work and i want to print the contents of AX
答案 0 :(得分:2)
; printf AX不起作用,我想打印AX的内容
您还没有向我们展示 PRINTf 应该做的事情。因此,您的所有PRINTf AX
宏调用都可以。
但是你的程序确实有2个错误阻止它正确执行。
答案 1 :(得分:1)
您可以按原样显示AX,但您会在屏幕上看到奇怪的字符。需要从二进制(AX)转换为字符串的过程。下一个代码将一个数字放入AX,将AX转换为字符串,并显示该字符串。您可以使用 number2string 过程为您的未来计划。复制粘贴EMU8086中的下一个代码并运行它:
.stack 100h
;------------------------------------------
.data
str db 6 dup('$') ;STRING TO STORE NUMBER.
;------------------------------------------
.code
;INITIALIZE DATA SEGMENT.
mov ax, @data
mov ds, ax
;CONVERT NUMBER TO STRING.
mov ax, 10382 ;ANY NUMBER.
call number2string ;CONVERT AX. RESULT IN "STR".
;DISPLAY STRING.
mov ah, 9
mov dx, offset str ;NUMBER CONVERTED.
int 21h
;WAIT FOR USER TO PRESS ANY KEY.
mov ah,7
int 21h
;FINISH PROGRAM.
mov ax, 4c00h
int 21h
;------------------------------------------
;NUMBER TO CONVERT MUST ENTER IN AX.
;ALGORITHM : EXTRACT DIGITS ONE BY ONE, STORE
;THEM IN STACK, THEN EXTRACT THEM IN REVERSE
;ORDER TO CONSTRUCT STRING.
proc number2string
mov bx, 10 ;DIGITS ARE EXTRACTED DIVIDING BY 10.
mov cx, 0 ;COUNTER FOR EXTRACTED DIGITS.
cycle1:
mov dx, 0 ;NECESSARY TO DIVIDE BY BX.
div bx ;DX:AX / 10 = AX:QUOTIENT DX:REMAINDER.
push dx ;PRESERVE DIGIT EXTRACTED FOR LATER.
inc cx ;INCREASE COUNTER FOR EVERY DIGIT EXTRACTED.
cmp ax, 0 ;IF NUMBER IS
jne cycle1 ;NOT ZERO, LOOP.
;NOW RETRIEVE PUSHED DIGITS.
mov si, offset str
cycle2:
pop dx
add dl, 48 ;CONVERT DIGIT TO CHARACTER.
mov [ si ], dl
inc si
loop cycle2
ret
endp
更多解释:对于您将来的程序,程序 number2string 需要一个名为" str"的数据段中的变量。正如您在代码中看到的那样," str"长度为6,因为AX可以容纳5位数或更少的数字,如果你想显示它,字符串需要' $'标志,这就是长度为6的原因。 number2string 的参数必须放在AX上。当然,您可以更改变量名称(str)和使用的寄存器(AX)。