xiranda/test_stack.asm

140 lines
4.1 KiB
NASM
Raw Normal View History

2024-04-01 07:23:49 -04:00
linux_read_system_call = 0
linux_write_system_call = 1
linux_open_system_call = 2
linux_close_system_call = 3
linux_exit_system_call = 60
standard_input = 0
standard_output = 1
format ELF64 executable 3
segment readable executable
main:
nop
nop
nop
mov rdi, text_data ; text);
call echo ; echo (
loop_1:
cmp [main_x], 16
jnb loop_1e
xor rdx, rdx
mov eax, [main_x]
mov ebx, 15
idiv rbx
cmp edx, 0
jne n3
mov rdi, fb
call echo
jmp n0
n3:
xor rdx, rdx
mov eax, [main_x]
mov ebx, 3
idiv rbx
cmp edx, 0
jne n1
mov rdi, f
call echo
jmp n0
n1:
xor rdx, rdx
mov eax, [main_x]
mov ebx, 5
idiv rbx
cmp edx, 0
jne n2
mov rdi, b
call echo
jmp n0
n2:
mov rdi, n
call echo
n0:
inc [main_x]
jmp loop_1
loop_1e:
;
mov rax, linux_exit_system_call ; <main> return;;
xor rdi, rdi
syscall
;~string_length (character * text):
;~natural length = 0;
;~loop (* (text + length++));
;~return (length);;
;~48 C7 C7 BD 11 40 00 E8 CA 00 00 00 83 3D F3 10 00 00 10 0F 83 83 00 00 00 48 31 D2 8B 05 E4 10 00 00 BB 0F 00 00 00 48 F7 FB 83 FA 00 75 0E 48 C7 C7
;~CA 11 40 00 E8 9B 00 00 00 EB 54 48 31 D2 8B 05 C0 10 00 00 BB 03 00 00 00 48 F7 FB 83 FA 00 75 0E 48 C7 C7 D4 11 40 00 E8 77 00 00 00 EB 30 48 31 D2
;~8B 05 9C 10 00 00 BB 05 00 00 00 48 F7 FB 83 FA 00 75 0E 48 C7 C7 DA 11 40 00 E8 53 00 00 00 EB 0C 48 C7 C7 E0 11 40 00 E8 45 00 00 00 FF 05 6F 10 00
;~00 E9 70 FF FF FF 48 C7 C0 3C 00 00 00 48 31 FF 0F 05
;~57 C8 00 00 00 C7 05 48 10 00 00 00 00 00 00 48 31 C0 80 3F 00 74 0B FF 05 3A 10 00 00 48 FF C7 EB F0 C9 8B 05 2E 10 00 00 5F C3
;~57 C8 00 00 00 E8 C5 FF FF FF 48 89 C2 48 89 FE 48 C7 C7 01 00 00 00 48 C7 C0 01 00 00 00 0F 05 48 31 C0 C9 5F C3
;~00 00 00 00 01 00 00 00 48 65 79 6F 20 77 6F 72 6C 64 21 0A 00 66 69 7A 7A 62 75 7A 7A 0A 00 66 69 7A 7A 0A 00 62 75 7A 7A 0A 00 6E 75 6D 62 65 72 0A 00
;~string_length (character * string) > natural:
;~length: natural = 0;
;~loop (string [length]) length++;
;~return (length);;
string_length: ; string_length
nop
nop
nop
push rdi
enter 0, 0
mov [string_length_length], 0
xor rax, rax
loop_0: ; loop_0
cmp byte [rdi], 0 ; (* (text + length)) != '\0'
je loop_0e
inc [string_length_length] ; text + length
inc rdi ; length++
jmp loop_0 ; if not go loop_0
loop_0e:
leave
mov eax, [string_length_length]
pop rdi
ret ; ;
;~echo (character * text):
;~system (linux_write_system_call, standard_output, text, string_length (text));
;~return;;
echo: ; echo )
nop
nop
nop
push rdi
enter 0, 0
call string_length ; |
mov rdx, rax ; | string_length (text)
mov rsi, rdi ; text
mov rdi, standard_output ; standard_output
mov rax, linux_write_system_call ; linux_write_system_call
syscall ; system (
xor rax, rax ; return;;
leave
pop rdi
ret ;
nop
nop
nop
segment readable writable
; variable
string_length_length dd 0 ; natural length = 0;
main_x dd 1 ; integer x = 0;
; data
text_data db 'Heyo world!', 10, 0
fb db 'fizzbuzz', 10, 0
f db 'fizz', 10, 0
b db 'buzz', 10, 0
n db 'number', 10, 0