A
A
Andrey Andryushchenko2017-05-27 21:06:22
linux
Andrey Andryushchenko, 2017-05-27 21:06:22

How to display the value of a number in ARM assembler?

There is code that prints "Hello, world." to the console.

.syntax unified
.data
message:
        .asciz "Hello, world."
len = . - message
.text
        .global _start
_start:
        mov     r0, $1
        ldr     r1, =message
        ldr     r2, =len
        mov     r7, $4
        swi     $0

        mov     r0, $0
        mov     r7, $1
        swi     $0

How to print number values ​​to console? For example, a register value. Preferably in decimal SS, but also in binary

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Karpion, 2019-03-05
@Karpion

Our register is 32-bit, it can take values ​​up to 4'294'967'296, which is only ten digits. So, we reserve an array of 11 bytes, the last one is filled with zero (i.e. '\0' - here is a backslash); for some reasons, it makes sense to take 12 bytes, then before the numbers there will certainly be a space separating the numbers.
Or it is not necessary to score the last byte with zero if the size of the string is transmitted there.
Then we take the register, divide it by ten.
The remainder of the branch is the last digit. We add to this remainder from the division '0' (digit code "zero" - without backslash) - we get the character of the last "letter" of our string.
We place the integer part of the division in the same register, and repeat the operation in the loop - we get the penultimate digit, and so on.
When the integer part of the division becomes zero, then we fill everything up to the beginning of the line with spaces.
With hexadecimal digits, it's both easier and more difficult.
Easier to share. The remainder of the division is obtained by the operation "AND 0x0F", the integer part of the division is obtained by shifting "<< 4".
The hardest part is getting the numbers. From "0" to "9" - are obtained by adding the same '0', but from "A" to "F" you need to add "'A'-0xA".
And to unpack a line - we are able.
PS: I'm getting old, I'm slowly forgetting the subtleties... And once I actively loved this business...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question