用汇编语言打印十进制数?
Printing decimal number in assembly language?
我正在尝试以十进制形式获取输出。请告诉我如何才能以十进制而不是 ASCII 获取相同的变量。
.model small
.stack 100h
.data
msg_1 db 'Number Is = $'
var_1 db 12
.code
add_1 proc
mov ax, @data
mov ds, ax
mov ah, 09
lea dx, msg_1
int 21h
mov ah, 02
mov dl, var_1
int 21h
mov ah, 4ch
int 21h
add_1 endp
end add_1
你写的这 3 行:
mov ah, 02
mov dl, var_1
int 21h
打印由 var_1 变量中保存的 ASCII 码表示的 字符。
要打印十进制数,您需要进行转换。
var_1 变量的值足够小 (12),可以使用特制代码来处理 0 到 99 之间的数字。代码使用 the AAM
instruction 轻松除以 10。
add ax, 3030h
进行真正的字符转换。它之所以有效,是因为“0”的 ASCII 代码是 48(十六进制的 30h),并且因为所有其他数字都使用下一个更高的 ASCII 代码:“1”是 49,“2”是 50,...
mov al, var_1 ; Your example (12)
aam ; -> AH is quotient (1) , AL is remainder (2)
add ax, 3030h ; -> AH is "1", AL is "2"
push ax ; (1)
mov dl, ah ; First we print the tens
mov ah, 02h ; DOS.PrintChar
int 21h
pop dx ; (1) Secondly we print the ones (moved from AL to DL via those PUSH AX and POP DX instructions
mov ah, 02h ; DOS.PrintChar
int 21h
如果您对打印大于 99 的数字感兴趣,那么
看看我发布的一般解决方案
Displaying numbers with DOS
我正在尝试以十进制形式获取输出。请告诉我如何才能以十进制而不是 ASCII 获取相同的变量。
.model small
.stack 100h
.data
msg_1 db 'Number Is = $'
var_1 db 12
.code
add_1 proc
mov ax, @data
mov ds, ax
mov ah, 09
lea dx, msg_1
int 21h
mov ah, 02
mov dl, var_1
int 21h
mov ah, 4ch
int 21h
add_1 endp
end add_1
你写的这 3 行:
mov ah, 02 mov dl, var_1 int 21h
打印由 var_1 变量中保存的 ASCII 码表示的 字符。
要打印十进制数,您需要进行转换。
var_1 变量的值足够小 (12),可以使用特制代码来处理 0 到 99 之间的数字。代码使用 the AAM
instruction 轻松除以 10。
add ax, 3030h
进行真正的字符转换。它之所以有效,是因为“0”的 ASCII 代码是 48(十六进制的 30h),并且因为所有其他数字都使用下一个更高的 ASCII 代码:“1”是 49,“2”是 50,...
mov al, var_1 ; Your example (12)
aam ; -> AH is quotient (1) , AL is remainder (2)
add ax, 3030h ; -> AH is "1", AL is "2"
push ax ; (1)
mov dl, ah ; First we print the tens
mov ah, 02h ; DOS.PrintChar
int 21h
pop dx ; (1) Secondly we print the ones (moved from AL to DL via those PUSH AX and POP DX instructions
mov ah, 02h ; DOS.PrintChar
int 21h
如果您对打印大于 99 的数字感兴趣,那么 看看我发布的一般解决方案 Displaying numbers with DOS