Skip to content

Instantly share code, notes, and snippets.

@hikiko
Last active July 26, 2019 19:24
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 hikiko/c726b5d81e8cdd400ebb5d18e77f9c57 to your computer and use it in GitHub Desktop.
Save hikiko/c726b5d81e8cdd400ebb5d18e77f9c57 to your computer and use it in GitHub Desktop.
ASM helloworld (nasm)
;32bit x86 code
[bits 32]
[section .text]
global _start
_start:
;linux specific
;interrupt 80hex for system calls
;4 = write /usr/include/x86_64-linux-gnu/asm/unistd_32.h
;1 = file descriptor of STDOUT
;13 = number of characters
;calc string size
mov eax, hellostr
call strlen
mov edx, eax
mov eax, 4
mov ebx, 1
mov ecx, hellostr
int 0x80
;print reversed
mov eax, hellostr
call printrev
;print newline
push byte 10
mov eax, 4
mov ebx, 1
;stack pointer esp has a newline
mov ecx, esp
mov edx, 1
int 0x80
;remove the newline from the stack
inc esp
;1 = exit system call
;0 = status (return 0 in c)
mov eax, 1
mov ebx, 0
int 0x80
strlen:
;argument in eax (ptr to string)
;string in edx
mov edx, eax
;dec eax value -1
dec eax
count_loop:
inc eax
;[] dereference (read eax data)
;1 byte bl
mov bl, [eax]
;compare (= subtract but not write)
cmp bl, 0
;if 0 flag not set (jump not zero)
jnz count_loop
;eax = eax - edx
sub eax, edx
;pops return address from the stack and jumps there
ret
printrev:
;arg = ptr to str
mov esi, eax
call strlen
;eax has the length
add esi, eax
print_loop:
dec esi
push eax
;call write for the character
mov eax, 4
mov ebx, 1
mov ecx, esi
mov edx, 1
int 0x80
pop eax
dec eax
jnz print_loop
ret
[section .data]
;10 = newline ASCII on nasm
;0 = 0 terminated string
hellostr: db "hello world!", 10, 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment