Skip to content

Instantly share code, notes, and snippets.

@ptantiku
Created February 22, 2015 14:41
Show Gist options
  • Save ptantiku/4bb81570db0204e0403c to your computer and use it in GitHub Desktop.
Save ptantiku/4bb81570db0204e0403c to your computer and use it in GitHub Desktop.
Hello World in GNU Assembly
.data [5/1908]
hellostr:
.string "Hello World"
.text
.global main
main:
pushq %rbp
movq %rsp, %rbp
print_word:
movq $hellostr, %rdi
call puts
print_int:
movq $123, %rax #print 123
movq %rsp, %rdi #save current stack address as rdi
push $0 #make room for result string (for 8 bytes = 8 digits)
dec %rdi #first byte above stack
movb $0, (%rdi) #store string terminator at this location
movb $10, %bl #set divider at 10 for each digit
next_digit:
idivb %bl # %ax/%bl ==> %al (quotient), %ah (reminder)
add $0x30, %ah # convert reminder to ascii by adding with '0'
dec %rdi # move back one byte
movb %ah, (%rdi) # store the digit
xor %ah, %ah # remove the reminder
test %al, %al # test if quotient is 0
jnz next_digit # do until %rax==0
call puts # done converting, call puts
pop %rax # clear the empty space
movq $0, %rax
leave
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment