Skip to content

Instantly share code, notes, and snippets.

@kylelk
Created May 9, 2015 02:14
Show Gist options
  • Save kylelk/3d8e596200594b8e4d37 to your computer and use it in GitHub Desktop.
Save kylelk/3d8e596200594b8e4d37 to your computer and use it in GitHub Desktop.
add two numbers in ARM assembly
.text
.equ inputBufferSize, 100
.global main
main:
ldr r0, =inputString
bl get_input
bl parseNumber
ldr r1, =number1
str r0, [r1]
ldr r0, =inputString
bl get_input
bl parseNumber
ldr r1, =number2
str r0, [r1]
ldr r2, =number1
ldr r2, [r2]
ldr r3, =number2
ldr r3, [r3]
add r1, r2, r3
ldr r0, =numberFormat
bl printf
mov r7, #1
swi 0
parseNumber:
push {r1-r4, lr}
mov r3, r0
bl length
mov r2, r0
sub r2, #1
mov r1, r3
mov r4, #1 @ place value
mov r5, #0 @ value
.parseNumber_loop:
ldrb r0, [r1, r2]
push {r1-r3}
sub r1, r0, #'0'
mul r2, r4, r1
add r5, r2
mov r1, #10
mul r2, r4, r1
mov r4, r2
pop {r1-r3}
sub r2, #1
cmp r2, #0
bge .parseNumber_loop
mov r0, r5
pop {r1-r4, lr}
mov pc, lr
length:
push {r1-r3, lr}
mov r3, r0
.length_loop:
ldrb r1, [r0, #1]!
cmp r1, #0
bne .length_loop
sub r0, r3
pop {r1-r3, lr}
mov pc, lr
get_input:
push {r0-r7, lr}
mov r1, r0
mov r0, #0
mov r2, #inputBufferSize
mov r7, #3
swi 0
mov r0, r1
mov r2, r0
bl length
sub r1, r0, #1
mov r0, #0
strb r0, [r2, r1]
pop {r0-r7, lr}
mov pc, lr
.data
inputString: .space inputBufferSize
inputText: .asciz "12345"
numberFormat: .asciz "%d\n"
number1: .word 0
number2: .word 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment