Skip to content

Instantly share code, notes, and snippets.

@hikilaka
Created September 7, 2015 05:52
Show Gist options
  • Save hikilaka/61b579df7ff89011857a to your computer and use it in GitHub Desktop.
Save hikilaka/61b579df7ff89011857a to your computer and use it in GitHub Desktop.
.data
.balign 4
strbuf: .skip 25
.set strbuf_sz, . - strbuf
.balign 4
newline: .ascii "\n"
.set newline_sz, . - newline
.text
.global _start
; TODO: possibly a div10 macro??
.macro mod aa, bb
mod_\@:
cmp \aa, \bb
subge \aa, \bb
bge mod_\@
.endm
; in:
; r0 = in_ptr
; r1 = len
; out: n/a
string_reverse:
add r1, r0
sub r1, #1
string_reverse_loop:
cmp r1, r0
bxlt lr
ldrb r2, [r0]
ldrb r3, [r1]
strb r3, [r0], #1
strb r2, [r1], #-1
b string_reverse_loop
; in:
; r0 = int
; r1 = buf
; out: n/a
int_to_string:
mov r2, r1
mov r3, #0
ldr r4, =429496730
cmp r0, #0
bge int_to_string_convert
mov r3, #45 ; add minus symbol if < 0
neg r0, r0
int_to_string_convert:
mov r5, #48 ; '0'
mov r6, r0
mod r6, #10
add r5, r6
strb r5, [r1], #1
umull r5, r6, r0, r4
mov r0, r6
cmp r0, #0
bgt int_to_string_convert
cmp r3, #0
beq int_to_string_done
strb r3, [r1], #1
int_to_string_done:
push {lr}
mov r0, r2
sub r1, r2
bl string_reverse
pop {lr}
bx lr
_start:
mov r0, #100
mov r1, #100
mul r0, r1
ldr r1, =strbuf
ldr r2, =strbuf_sz
bl int_to_string
mov r0, #1
ldr r1, =strbuf
ldr r2, =strbuf_sz
mov r7, #4
swi #0
mov r0, #1
ldr r1, =newline
ldr r2, =newline_sz
mov r7, #4
swi #0
mov r0, #0
mov r7, #1
swi #0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment