Skip to content

Instantly share code, notes, and snippets.

@jeffpeterson
Created March 25, 2013 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffpeterson/5241315 to your computer and use it in GitHub Desktop.
Save jeffpeterson/5241315 to your computer and use it in GitHub Desktop.
! Definition for the 0 in `ta 0`:
! /usr/include/sys/trap.h
! Definition for the 4 in `mov 4, %g1`
! /usr/include/sys/syscall.h
.global main
.section ".data" ! We need read/write memory
format_string:
.asciz "Baby, you got a stew going!%u I think I want my money back."
.align 4 ! Align to a word boundary
.section ".text" ! Back to read-only memory.
main:
save %sp, -1024, %sp ! Create a new stack frame.
set format_string, %o1 ! Start at format_string
mov 0, %o2 ! This is our current length
next_character:
ldub [%o1 + %o2], %l0 ! Grab one character.
cmp %l0, '%' ! Do we have a '%'?
bne,a next_character ! Loop again if we don't
inc %o2 ! Increment the length
mov '\n', %l0 ! Get a newline ready to store
stub %l0, [%o1 + %o2] ! Replace '%' with newline
inc %o2
mov 4, %g1 ! 4 is SYS_write.
mov 1, %o0 ! We're writing to stdout.
ta 0 ! Ask the kernel for a system call
mov 0, %i0 ! Return zero to the shell
ret
restore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment