本文介绍了如何打印一个长整型,我在一个寄存器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义了一个长整型如下:

Then, I am adding to it with something like this:

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:

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.)

这篇关于如何打印一个长整型,我在一个寄存器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:42