Skip to content

Instantly share code, notes, and snippets.

@Davenchy
Created October 25, 2025 09:11
Show Gist options
  • Select an option

  • Save Davenchy/a6e4bca7de23397ac8b87169c7b4d02c to your computer and use it in GitHub Desktop.

Select an option

Save Davenchy/a6e4bca7de23397ac8b87169c7b4d02c to your computer and use it in GitHub Desktop.
My first x86-x64 ASM program
;#---------------------
;# GNU Assembler file
;# Function calls demo
;#---------------------
.intel_syntax noprefix
.global _start
.text
_start:
mov rbp, rsp
mov rdi, 5
mov rsi, 10
call add
mov rsi, rax
call print_val
call exit
;# rsi: the string to measure its length
;# rax: the measured length
strlen:
xor rax, rax
xor rbx, rbx
1:
mov bl, byte ptr [rsi + rax]
cmp bl, 0
je 2f
inc rax
jmp 1b
2:
ret
;# rdi: a
;# rsi: b
;# rax: the calculated value
add:
mov rax, rdi
add rax, rsi
ret
;# rdi: write to
;# rsi: value to convert
itoa:
mov rax, rsi
mov rbx, 0x0a
xor rcx, rcx
1:
xor rdx, rdx
div rbx
add rdx, 0x30
mov byte ptr [rdi + rcx], dl
inc rcx
cmp rax, 0
jne 1b
mov byte ptr [rdi + rcx], 0
mov rsi, rdi
lea rdi, [rdi + rcx - 1]
call strrev
ret
;# rsi: string to reverse
;# rdi: the end of the string to reverse
strrev:
cmp rsi, rdi
jae 1f
mov al, [rsi]
mov bl, [rdi]
mov [rsi], bl
mov [rdi], al
inc rsi
dec rdi
jmp strrev
1:
ret
;# rdi: dist
;# rsi: src
;# rax: the length of copied bytes
strcpy:
xor rax, rax
1:
mov bl, byte ptr [rsi + rax]
cmp bl, 0
je 2f
mov byte ptr [rdi + rax], bl
inc rax
jmp 1b
2:
ret
;# rsi: value to print
print_val:
push rbp
mov rbp, rsp
sub rsp, 32
buf = -31
buf_len = -32
;# store the value
push rsi
;# copy prefix msg
lea rdi, [rbp + buf]
lea rsi, msg
call strcpy
mov [rbp + buf_len], al
;# restore the value
pop rsi
;# convert value to string and append to the buffer
lea rdi, [rbp + buf]
movzx rax, byte ptr [rbp + buf_len]
add rdi, rax
call itoa
;# measure the new buffer length
lea rsi, [rbp + buf]
call strlen
;# write the buffer to the stdout
mov rdi, 1
mov dl, al
call write
mov rsp, rbp
pop rbp
ret
;# rdi: fd
;# rsi: to write
;# rdx: length in bytes
write:
mov rax, 1
syscall
ret
exit:
mov rax, 60
xor rdi, rdi
syscall
.data
msg: .asciz "Value: "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment