如何打印一个长整型,我在一个寄存器? [英] How do I print a long integer that I have in a register?
问题描述
我已经定义了一个长整型如下:
memTotal:0。长期
块引用>
块引用>然后,我加入到它的东西是这样的:
12 ADDL(%DI),ECX%
块引用>
块引用>在哪里我再往前%ECX到memTotal。我的问题是,我将如何去在memTotal的MB计算的大小。我试图沿着线的东西:
SHRL $ 20%EAX
块引用>
块引用>不过,我怎么会那么打印在INT为MB?
我在正确的轨道上吗?任何帮助是AP preciated。
解决方案我假设你知道如何打印单个ASCII字符。所以,现在你需要一个算法来从一个整数提取数字
A
- 我会提供一个,我觉得是很容易理解和容易扩展(这是不一定的最好的的)。
- 计算
B:=一%10
。B
是你的号码的最后一位数字- 设置
A:= A / 10
(整数除法)- 从开始重复获得第二至最后一位数字,等停止时
A == 0
。一旦你有一个数字的值,你可以添加一个固定常数来获得它的ASCII值,您可以使用打印相应的字符。
以上可以打印一个数字。不幸的是,在这一点上,你只能向后打印。为了解决这个问题,分配一个字符串,你可以把数字从右到左。通过分配一个字符串,将大到足以容纳开始的所有的长整型,然后拿出上面的算法,将跟踪的多少位有,并据此分配一个字符串的修改。
(是的,
SHRL $ 20%EAX
听起来似乎在理车削字节到通常所说的MIB。约定有所不同,但我认为,通常一个是1 MB = 10 ^ 6个字节,1 MIB = 2 ^ 20字节)。I have defined a long integer as follows:
memTotal: .long 0
Then, I am adding to it with something like this:
addl 12(%di), %ecx
Where I then move %ecx to memTotal. My question is, how would I go about calculating the size in MB of the memTotal. I tried something along the lines of:
shrl $20, %eax
But how would I then print that as in int for MB?
Am I on the right track? Any help is appreciated.
解决方案I assume you know how to print a single ASCII character. So now you need an algorithm to extract the digits from an integer
a
- I will provide one that I think is easy to understand and easy to expand (it's not necessarily the best).
- calculate
b := a%10
.b
is the last digit of your number- set
a := a/10
(integer division)- repeat from beginning to get second-to-last digit, etc. Stop when
a == 0
.Once you have the value of a digit, you can add a fixed constant to get its ASCII value, which you can use to print the corresponding character.
The above enables you to print a number. Unfortunately, at this point you can only print it backwards. In order to fix that, allocate a string in which you can put the digits from right to left. Start by allocating a string that will be large enough to hold all long ints, then come up with a modification of the above algorithm that will keep track of how many digits there are, and allocate a string accordingly.
(And yes,
shrl $20, %eax
sounds sensible for turning bytes into what is usually called MiBs. Conventions vary, but I think the usual one is 1 MB = 10^6 bytes, and 1 MiB = 2^20 bytes.)这篇关于如何打印一个长整型,我在一个寄存器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!