How to print numbers greater then 10 in ARM200 Assembly language

前端 未结 2 1226
名媛妹妹
名媛妹妹 2021-01-26 04:30

We are using ARM200 to learn assembly language. I have a portion of memory with 32 integers filling it. I need to be able to print out these 32 integers to the screen. I can

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-26 04:44

    You could do so with a simple loop, dividing your number by 10 and storing the remainder each time, until the number finally gets to 0. You'll basically end up with an array of numbers from 0-9 which you can then print one at a time. You have to store them before printing because you'll end up with each digit in reverse order. eg:

    Number | Buffer
    123    | { EMPTY }
    12     | 3         (123 / 10 = 12, remainder 3)
    1      | 3 2       (12 / 10 = 1, remainder 2)
    0      | 3 2 1     (1 / 10 = 0, remainder 1)
    

    Each time you add a number to the buffer, increment your counter. Once you've finished dividing and your number is now 0, you can then start printing. Loop from count to 0, printing out the number stored at Buffer + count.

    For the buffer, if you're saying that each number can be up to 4 bytes (32 bits), than you know that in decimal the largest number that can be represented is either 2147483647 (signed) or 4294967295 (unsigned). In both cases, the largest most amount of digits is 10, so you can allocate a buffer of 10 bytes (1 byte is enough to hold each digit from 0-9).

    Another alternative (which I've done before doing the same task as you for a different chip) is to use the stack rather than have a buffer allocated, and push each digit onto the stack at each iteration in the loop. Both ways are fairly simple.

    I'll let you come up with the code, since you're meant to be learning how to do it.

    Edit: This is some pseudo code of the common method I described above:

    Buffer[10]
    
    Num = 123
    
    count = 0
    // Split the number into an array of digits
    WHILE Num IS NOT 0
       Buffer[count] = Num % 10 // Store the remainder
       Num = Num / 10
       count++
    
    count-- // count will be 1 more than the amount in Buffer
    
    // Print the digits
    WHILE count IS >= 0
       PRINT Buffer[count]
       count--
    

提交回复
热议问题