Last active
May 4, 2022 19:52
-
-
Save ammarfaizi2/e2dbdf5980d6b19159fc88c84c8949ae to your computer and use it in GitHub Desktop.
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
; This is a program to reverse string. | |
; Link System V ABI x86-64: https://gitlab.com/x86-psABIs/x86-64-ABI/-/wikis/home | |
; Link discussion: https://t.me/GNUWeeb/583153 | |
section .data | |
INPUT db "QWERTYUIOPASDFGHJKL", 0 | |
section .text | |
global _start | |
_start: | |
and rsp, -16 | |
xor ebp, ebp | |
call main | |
mov edi, eax | |
mov eax, 60 | |
syscall | |
; int main(void); | |
main: | |
sub rsp, 8 | |
mov rdi, INPUT | |
call printReverseString | |
call printNewLine | |
xor eax, eax | |
add rsp, 8 | |
ret | |
; size_t strlen(const char *str); | |
strlen: | |
xor eax, eax | |
cmp byte [rdi], 0 | |
je .strlen_out | |
.strlen_loop: | |
inc rax | |
cmp byte [rdi + rax], 0 | |
jne .strlen_loop | |
.strlen_out: | |
ret | |
; void printString(const char *str); | |
printString: | |
push rbp | |
mov rbp, rdi | |
call strlen | |
; syscall write(1, str, length) | |
mov rdx, rax | |
mov eax, 1 | |
mov edi, 1 | |
mov rsi, rbp | |
syscall | |
pop rbp | |
ret | |
; void printReverseString(char *str); | |
printReverseString: | |
push rbp | |
sub rsp, 128 | |
mov rbp, rdi | |
call strlen | |
mov rsi, rax | |
xor ecx, ecx | |
.printReverseString_loop: | |
mov dil, [rbp + rax - 1] | |
mov [rsp + rcx], dil | |
inc rcx | |
dec rax | |
jnz .printReverseString_loop | |
mov byte [rsp + rcx], 0 | |
mov rdi, rsp | |
call printString | |
add rsp, 128 | |
pop rbp | |
ret | |
printNewLine: | |
mov byte [rsp - 1], 10 | |
mov edx, 1 | |
mov eax, 1 | |
mov edi, 1 | |
lea rsi, [rsp - 1] | |
syscall | |
ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment