用汇编打印十六进制数字

BSc*_*ker 4 x86 assembly masm nasm

我正在努力学习NASM程序集,但我似乎正在努力解决高级语言中的问题.

我正在使用的所有教科书都使用字符串进行讨论 - 实际上,这似乎是他们最喜欢的东西之一.打印你好世界,从大写改为小写等

但是,我试图了解如何在NASM程序集中增加和打印十六进制数字,并且不知道如何继续.例如,如果我想在Hex中打印#1 - n,那么如果不使用C库(我能够找到使用的所有引用),我该怎么办呢?

我的主要想法是在.data部分有一个变量,我将继续增加.但是如何从此位置提取十六进制值?我似乎需要先将它转换为字符串......?

任何建议或示例代码将不胜感激.

Pau*_*l R 8

First write a simple routine which takes a nybble value (0..15) as input and outputs a hex character ('0'..'9','A'..'F').

Next write a routine which takes a byte value as input and then calls the above routine twice to output 2 hex characters, i.e. one for each nybble.

Finally, for an N byte integer you need a routine which calls this second routine N times, once for each byte.

You might find it helpful to express this in pseudo code or an HLL such as C first, then think about how to translate this into asm, e.g.

void print_nybble(uint8_t n)
{
    if (n < 10) // handle '0' .. '9'
        putchar(n + '0');
    else // handle 'A'..'F'
        putchar(n - 10 + 'A');
}

void print_byte(uint8_t n)
{
    print_nybble(n >> 4); // print hi nybble
    print_nybble(n & 15); // print lo nybble
}

print_int16(uint16_t n)
{
    print_byte(n >> 8); // print hi byte
    print_byte(n & 255); // print lo byte
}
Run Code Online (Sandbox Code Playgroud)