Skip to content

Instantly share code, notes, and snippets.

@vipulbhj
Created May 20, 2026 18:00
Show Gist options
  • Select an option

  • Save vipulbhj/9b6c4aaef6bdec5295ea9aa0dc757227 to your computer and use it in GitHub Desktop.

Select an option

Save vipulbhj/9b6c4aaef6bdec5295ea9aa0dc757227 to your computer and use it in GitHub Desktop.
Expression Evaluator
.global _main
.text
parse_num:
mov x1, #0
1:
ldrb w2, [x0]
cmp w2, #'0'
blt 2f
cmp w2, #'9'
bgt 2f
sub w2, w2, #'0'
mov x3, #10
mul x1, x1, x3
add x1, x1, x2
add x0, x0, #1
b 1b
2:
ret
_main:
mov x4, #0 // Arthematic Accumulator
adrp x1, buf@PAGE
add x1, x1, buf@PAGEOFF
mov x0, #0
mov x2, #256
mov x16, #3
svc #0x80 // x0 = bytes read
adrp x0, buf@PAGE
add x0, x0, buf@PAGEOFF // x0 now points to the buffer
bl parse_num
mov x4, x1
1:
add x0, x0, #1
ldrb w5, [x0]
add x0, x0, #2
bl parse_num
cmp w5, #'+'
beq do_add
cmp w5, #'-'
beq do_sub
cmp w5, #'*'
beq do_mul
cmp w5, #'/'
beq do_div
2:
ldrb w5, [x0]
cmp w5, #' '
beq 1b
b done
do_add:
add x4, x4, x1
b 2b
do_sub:
sub x4, x4, x1
b 2b
do_mul:
mul x4, x4, x1
b 2b
do_div:
udiv x4, x4, x1
b 2b
done:
mov x0, x4
mov x16, #1
svc #0x80
.data
buf:
.space 256
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment