0

I am new to arm assembly, i'm trying to print integer to screen instead of their ASCII representation of that number. Below is the code which i'm trying with instead of printing 99 i'm getting 'c'.

.global _start
.text
_start:

@ write to num variable
_write:
    mov r8, #99
    ldr r9, =num
    str r8, [r9]

@read from num variable and print to screen
_read:
    mov r7, #4 @ syscall
    mov r0, #1 @ std output
    ldr r1, =num @ starting address
    mov r2, #2 @ number of character to print
    swi  0 @ interrupt

_end:
    mov r7, #1
    swi 0

.data
num: .word 0

I have checked online resources which has numerous examples of printing strings but not numbers.

  [root@alarmpi]# make
  as -o num.o num.s
  ld - num num.o
  [root@alarmpi ]# ./num
  c

Kindly let me know how to output numbers using ARM assembly (possibly without using C libraries).

Thank You.

Kanan Jarrus
  • 607
  • 1
  • 12
  • 26
  • 2
    The number 99 has two digits. A 9, and then another 9. You need to account for this in your solution. I [gave an answer here that might be relevant to you](https://stackoverflow.com/a/56328715/1517578). While not ARM, the problem and solution are the same. – Simon Whitehead Aug 13 '19 at 06:42
  • terminology: what you *want* is a number represented as an ASCII string of decimal-digit characters. (i.e. ASCII decimal as a text serialization format for numbers). What you're getting is the ASCII character represent**ed** by your number, not the number's represent**ation**. The algorithm in general is repeated division by 10: see [How do I print an integer in Assembly Level Programming without printf from the c library?](//stackoverflow.com/a/46301894) for a C implementation (and x86 asm). – Peter Cordes Aug 13 '19 at 06:49
  • 1
    @SimonWhitehead & PeterCordes Thank you. I was able to print one number to the screen by adding 48 to it. In order to print multiple number i will have to learn to push them to buffer and the print them. Which i will learn soon enough. – Kanan Jarrus Aug 13 '19 at 12:25

0 Answers0