Skip to content

Instantly share code, notes, and snippets.

@aktau
Last active August 29, 2015 14:07
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 aktau/a85a925282fbe66d13b0 to your computer and use it in GitHub Desktop.
Save aktau/a85a925282fbe66d13b0 to your computer and use it in GitHub Desktop.
Hello world and printing numbers a few times
; also see an alternative: http://lpaste.net/111802
; rdi = the number (integer) to print
; prints a number as hex (basically the easy way out)
printnum:
push r13
push r14
push r15
; save the argument because we're going to use it to pass an argument to
; printchar (r13 is callee-save)
mov r13, rdi
; find the highest nibble
bsr r15, rdi ; get most significant bit set
and r15, ~0x03 ; truncate to nibble granularity (ex: 0110 0111 -> 0110 0100)
.loop:
; fetch a byte, mask it and shift
movzx ecx, r15b ; store the result in cl (from rcx)
mov rdi, r13
shr rdi, cl
and rdi, 0xF
cmp dil, 0xA
jge .letter
.number:
add dil, '0'
jmp .print
.letter:
add dil, 'A' - 0xA
.print:
; print the character and loop
call printchar
; decrease the variable shift and loop back if underflow
sub r15b, 4
jns .loop
pop r15
pop r14
pop r13
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment