How to print a number in Assembly 8086?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 12:35:02

问题


I'm trying to write a function that receives a number (which I pushed earlier), and prints it. How can I do it?

What I have so far:

org 100h

push 10
call print_num

print_num:

push bp
mov bp, sp
mov ax, [bp+2*2]
mov bx, cs
mov es, bx
mov dx, string
mov di, dx
stosw
mov ah, 09h
int 21h
pop bp
ret

string:

回答1:


What you're placing at the address of string is a numerical value, not the string representation of that value.

The value 12 and the string "12" are two separate things. Seen as a 16-bit hexadecimal value, 12 would be 0x000C while "12" would be 0x3231 (0x32 == '2', 0x31 == '1').

You need to convert the numerical value into its string representation and then print the resulting string.
Rather than just pasting a finished solution I'll show a simple way of how this could be done in C, which should be enough for you to base an 8086 implementation on:

char string[8], *stringptr;
short num = 123;

string[7] = '$';  // DOS string terminator    

// The string will be filled up backwards
stringptr = string + 6;

while (stringptr >= string) {
    *stringptr = '0' + (num % 10);  // '3' on the first iteration, '2' on the second, etc
    num /= 10;  // 123 => 12 => 1 => 0
    if (num == 0) break;
    stringptr--;
}        


来源:https://stackoverflow.com/questions/15204659/how-to-print-a-number-in-assembly-8086

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!