Created
October 16, 2022 20:47
-
-
Save worldOneo/dc0dafca6a0aaeb5b7774ec783be28bd to your computer and use it in GitHub Desktop.
print hex numbers in AT&T ASM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.global main | |
.set print_hex_msb4mask, 0xf000000000000000 | |
.data | |
print_hex_chars: | |
.ascii "0123456789abcdef" | |
.text | |
syscall4: | |
movq %rax, %rax | |
movq %rbx, %rdi | |
movq %rcx, %rsi | |
syscall | |
ret | |
print_stdout: | |
enter $0, $0 | |
pushq %rbx | |
pushq %rax | |
movq $1, %rax | |
movq $1, %rbx | |
popq %rcx | |
popq %rdx | |
call syscall4 | |
leave | |
ret | |
print_hex: | |
enter $0, $0 | |
cmp $0, %rax | |
jne .print_hex_skip | |
jmp .print_hex_end | |
.print_hex_skip: | |
movq %rax, %rbx | |
movq $print_hex_msb4mask, %rdx | |
andq %rdx, %rbx | |
cmpq $0, %rbx | |
jne .print_hex_skip_end | |
shlq $4, %rax | |
jmp .print_hex_skip | |
.print_hex_skip_end: | |
push %rax | |
.print_hex_loop: | |
popq %rax | |
cmpq $0, %rax | |
je .print_hex_end | |
pushq %rax | |
movq $print_hex_msb4mask, %rbx | |
andq %rbx, %rax | |
shrq $60, %rax | |
addq $print_hex_chars, %rax | |
movq $1, %rbx | |
call print_stdout | |
popq %rax | |
shlq $4, %rax | |
pushq %rax | |
jmp .print_hex_loop | |
.print_hex_end: | |
pop %rax | |
xor %rax, %rax | |
leave | |
ret | |
println: | |
enter $0,$0 | |
pushq $10 | |
mov %rsp, %rax | |
mov $1, %rbx | |
call print_stdout | |
leave | |
ret | |
main: | |
mov $0xBabeCafe, %rax | |
call print_hex | |
call println | |
mov $0xDeadBeef, %rax | |
call print_hex | |
call println | |
mov $60, %rax | |
mov $0, %rdi | |
syscall |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment