按照大会打印十六进制数字 [英] Printing Hexadecimal Digits with Assembly
问题描述
我想学习NASM组装,但我似乎什么似乎只是在高级语言挣扎。
I'm trying to learn NASM assembly, but I seem to be struggling with what seems to simply in high level languages.
所有这一切我使用使用字符串讨论教科书 - 这似乎是他们最喜欢的东西之一,其实。印刷世界你好,从大写改为小写,等等。
All of the textbooks which I am using discuss using strings -- in fact, that seems to be one of their favorite things. Printing hello world, changing from uppercase to lowercase, etc.
不过,我想了解如何递增,在NASM装配打印十六进制数字,不知道如何着手。举例来说,如果我想打印#1 - N的十六进制,我会怎么做,以便在不使用C库(所有引用我已经能够找到使用)
However, I'm trying to understand how to increment and print hexadecimal digits in NASM assembly and don't know how to proceed. For instance, if I want to print #1 - n in Hex, how would I do so without the use of C libraries (which all references I have been able to find use)?
我的主要想法是在.data段,我会继续增加的变量。但我怎么提取这个位置的十六进制值?我似乎需要先将其转换成字符串...?
My main idea would be to have a variable in the .data section which I would continue to increment. But how do I extract the hexadecimal value from this location? I seem to need to convert it to a string first...?
任何意见或样品code将AP preciated。
Any advice or sample code would be appreciated.
推荐答案
先写一个简单的程序,它接受一个半字节值(0..15)作为输入和输出一个十六进制字符('0'..'9' 'A'..'F')
First write a simple routine which takes a nybble value (0..15) as input and outputs a hex character ('0'..'9','A'..'F').
接下来写一个程序,它接受一个字节的值作为输入,然后调用上面的例行两次以输出2个十六进制字符,即一个用于每个半字节。
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.
最后,对于N字节的整数,你需要的每个字节调用此第二个程序N次,一次常规的。
Finally, for an N byte integer you need a routine which calls this second routine N times, once for each byte.
您可能会发现给前preSS这个伪code或HLL如C,然后再想想如何翻译成ASM,例如这一点。
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
}
这篇关于按照大会打印十六进制数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!