打印整数x86汇编安慰 [英] Print integer to console in x86 assembly
问题描述
当我在16位汇编两个值相加,什么是打印结果到控制台的最佳方式?
目前,我有这个code:
;; --- code START --- ;;
MOV AX,1;把1到AX
加斧,2;加2斧子的电流值
MOV啊,2; 2输出焦炭在DOS服务功能编号。
MOV DL,斧头; DL取值。
INT 21H;调用DOS服务MOV啊,4路; 4路是在DOS服务出口程序的功能编号。
INT 21H;功能4路并不关心在寄存器什么。
;; --- code END --- ;;
我觉得DL值应为ASCII code,但我不知道如何斧头附加值成ASCII后进行转换。
您基本上要10分,打印余数(个位数),然后用所得的商重复。
;假设号码是EAX
MOV ECX,10loophere:
MOV EDX,0
DIV ECX ;现在EAX< - EAX / 10
; EDX< - EAX 10% ;打印EDX
;这是一个数字,这是我们必须转换为ASCII
;打印程序使用EDX和EAX,让我们推动EAX
;入堆栈。在的开始时,我们清楚的EDX
;循环无论如何,所以我们没有,如果我们用它关心周围很多 推EAX ;转换分升ASCII
添加dl时,0 MOV啊,2; 2输出焦炭在DOS服务功能编号。
INT 21H;调用DOS服务 ;现在恢复EAX
流行EAX ;如果EAX是零,我们可以退出 CMP EAX,0
JNZ loophere
作为一个方面说明,你在你的code有一个错误在这里:
MOV AX,1;把1到AX
加斧,2;加2斧子的电流值
MOV啊,2; 2输出焦炭在DOS服务功能编号。
MOV DL,斧头; DL取值。
您把 2
在啊
,然后你把斧
在 DL
。你基本上废弃,另外添购斧
打印前。
您还可以自 DL
是8位宽,斧
为16位宽。大小不匹配p>
你应该做的是翻转的最后两行并固定大小不匹配:
MOV AX,1;把1到AX
加斧,2;加2斧子的电流值MOV DL,人; DL取值。
MOV啊,2; 2输出焦炭在DOS服务功能编号。
When I add two values in 16 bit assembly, what is the best way to print the result to console?
At the moment I have this code:
;;---CODE START---;;
mov ax, 1 ;put 1 into ax
add ax, 2 ; add 2 to ax current value
mov ah,2 ; 2 is the function number of output char in the DOS Services.
mov dl, ax ; DL takes the value.
int 21h ; calls DOS Services
mov ah,4Ch ; 4Ch is the function number for exit program in DOS Services.
int 21h ; function 4Ch doesn't care about anything in the registers.
;;---CODE END---;;
I think that dl value should be in ASCII code, but I'm not sure how to convert ax value after addition into ASCII.
You basically want to divide by 10, print the remainder (one digit), and then repeat with the quotient.
; assume number is in eax
mov ecx, 10
loophere:
mov edx, 0
div ecx
; now eax <-- eax/10
; edx <-- eax % 10
; print edx
; this is one digit, which we have to convert to ASCII
; the print routine uses edx and eax, so let's push eax
; onto the stack. we clear edx at the beginning of the
; loop anyway, so we don't care if we much around with it
push eax
; convert dl to ascii
add dl, '0'
mov ah,2 ; 2 is the function number of output char in the DOS Services.
int 21h ; calls DOS Services
; now restore eax
pop eax
; if eax is zero, we can quit
cmp eax, 0
jnz loophere
As a side note, you have a bug in your code right here:
mov ax, 1 ;put 1 into ax
add ax, 2 ; add 2 to ax current value
mov ah,2 ; 2 is the function number of output char in the DOS Services.
mov dl, ax ; DL takes the value.
You put 2
in ah
, and then you put ax
in dl
. You're basically junking ax
before printing it.
You also have a size mismatch since dl
is 8 bits wide and ax
is 16 bits wide.
What you should do is flip the last two lines and fix the size mismatch:
mov ax, 1 ;put 1 into ax
add ax, 2 ; add 2 to ax current value
mov dl, al ; DL takes the value.
mov ah,2 ; 2 is the function number of output char in the DOS Services.
这篇关于打印整数x86汇编安慰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!