Skip to content

Instantly share code, notes, and snippets.

@t-sin
Last active February 27, 2022 13:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t-sin/a73596332bb23d08909f2d9644d8ccd2 to your computer and use it in GitHub Desktop.
Save t-sin/a73596332bb23d08909f2d9644d8ccd2 to your computer and use it in GitHub Desktop.
my first (ARMv6) assembly program

ARMv6 assembly language study

to run

on Raspberry PI (Raspbian)

as -o test.o test.s && ld -o test test.o && ./test

on x86 Ubuntu

  1. install tools:
sudo apt install qemu qemu-user-static gcc-arm-linux-gnueabi
  1. assemble and link program
arm-linux-gnueabi-as -o test.o test.s && arm-linux-gnueabi-ld -o test test.o
  1. run on qemu:
qemu-arm-static -L /usr/arm-linux-gnueabi ./test
.data
prompt:
.string "input something... > "
prompt_len = . - prompt
buffer:
.skip 256
buffer_len = . - buffer
.text
.global _start
sys_write:
mov r7, #4 // sys_write
svc #0
bx lr
sys_read:
mov r7, #3 // sys_read
svc #0
bx lr
_start:
mov r0, #1 // stdout
ldr r1, =prompt
mov r2, #prompt_len
bl sys_write
mov r0, #0 // stdin
ldr r1, =buffer
mov r2, #buffer_len
bl sys_read
mov r3, r0 // save the return value of sys_read
mov r2, r0
mov r0, #1 // stdout
ldr r1, =buffer
bl sys_write
cmp r3, #0 // unless sys_read returns EOF then go next loop
blne _start
end:
mov r7, #1 // sys_exit
mov r1, #0
svc #0
.data
hello:
.byte 0x68 // h
.byte 0x6f // o
.byte 0x67 // g
.byte 0x65 // e
.byte 0x0a // newline
hello_len = . - hello
.text
.global _start
_start:
ldr r0, =hello
mov r1, #42
strb r1, [r0, #1]
mov r0, #1 // file discripter
ldr r1, =hello
mov r2, #hello_len // character count
mov r7, #4 // sys_write
svc #0
mov r0, #0
mov r7, #1
svc #0 // sys_exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment