Get rid of unneeded function prologues/epilogues

This commit is contained in:
2025-06-26 07:23:29 +02:00
parent 3730086281
commit fc28643c1a
4 changed files with 6 additions and 103 deletions

View File

@ -19,24 +19,16 @@ section .text
;----- print (char* string) -----;
; return value: N/A
print:
push rbp
mov rbp, rsp
call strlen
mov rdx, rax
mov rax, NR_write
mov rsi, rdi
mov rdi, 1
syscall
leave
ret
;----- puts (char* string) -----;
; return value: N/A
puts:
push rbp
mov rbp, rsp
mov r10, rdi
call print
mov rdi, r10
@ -45,8 +37,6 @@ puts:
mov rsi, NL
mov rdx, 1
syscall
leave
ret
;----- printf(const char* string, ...) -----;
; Currently only supports specifiers: %d, %c, %s, %%

View File

@ -16,121 +16,78 @@ section .text
;----- min(int a, int b) -----;
; return value: lowest value
min:
push rbp
mov rbp, rsp
mov rax, rdi
cmp rdi, rsi
cmovg rax, rsi
leave
ret
;----- minu(unsigned int a, unsigned int b) -----;
; return value: lowest value
minu:
push rbp
mov rbp, rsp
mov rax, rdi
cmp rdi, rsi
cmova rax, rsi
leave
ret
;----- max(int a, int b) -----;
; return value: highest value
max:
push rbp
mov rbp, rsp
mov rax, rdi
cmp rdi, rsi
cmovl rax, rsi
leave
ret
;----- maxu(unsigned int a, unsigned int b) -----;
; return value: highest value
maxu:
push rbp
mov rbp, rsp
mov rax, rdi
cmp rdi, rsi
cmovb rax, rsi
leave
ret
;----- islower(char* c) -----;
; return value: 1 (character is in range of 'a'-'z') or 0 (character is nog in range of 'a'-'z')
islower:
push rbp
mov rbp, rsp
xor rax, rax
cmp byte [rdi], 'a'
jb .quit
cmp byte [rdi], 'z'
ja .quit
mov rax, 1
.quit:
leave
ret
;----- isupper(char* c) -----;
; return value: 1 (character is in range of 'A'-'Z') or 0 (character is not in range of 'A'-'Z')
isupper:
push rbp
mov rbp, rsp
xor rax, rax
cmp byte [rdi], 'A'
jb .quit
cmp byte [rdi], 'Z'
ja .quit
mov rax, 1
.quit:
leave
ret
;----- tolower(char* c) -----;
; return value: ASCII value. Same as original if it was not an uppercase letter, otherwise lowercase letter
tolower:
push rbp
mov rbp, rsp
call isupper
mov rcx, rax
mov al, byte [rdi]
cmp rcx, 1
jne .quit
add al, 32
.quit:
leave
ret
;----- toupper(char* c) -----;
; return value: ASCII value. Same as original if it was not a lowercase letter, otherwise uppercase letter
toupper:
push rbp
mov rbp, rsp
call islower
mov rcx, rax
mov al, byte [rdi]
cmp rcx, 1
jne .quit
sub al, 32
.quit:
leave
ret
;----- itoa(int num, char* str) -----;
; return value: pointer to str
itoa:
push rbp
mov rbp, rsp
push rbx
push rsi
push rdi
@ -178,7 +135,6 @@ itoa:
.quit:
pop rax
pop rbx
leave
ret
;----- exit(int exit_code) -----;
; returns: nothing

View File

@ -21,9 +21,6 @@ section .text
;----- fopen(const char *filename, const char mode) -----;
; return value: pointer to file or value < 0 (neg errno) if file failed to open
fopen:
push rbp
mov rbp, rsp
cmp sil, 'r'
je .setMode_r
cmp sil, 'w'
@ -63,14 +60,10 @@ fopen:
syscall
.quit:
leave
ret
;----- fclose(FILE* fp) -----;
; return value: 0 on success, below 0 otherwise (neg errno)
fclose:
push rbp
mov rbp, rsp
cmp rdi, 3
jl .ebadf
@ -82,15 +75,11 @@ fclose:
mov rax, -9
.quit:
leave
ret
;----- fwrite(FILE* fp, char* str) -----;
; writes a string to opened file
; return value: amount of written characters on success, below 0 otherwise (neg errno)
fwrite:
push rbp
mov rbp, rsp
cmp rdi, 3
jl .ebadf
@ -107,5 +96,4 @@ fwrite:
mov rax, -9
.quit:
leave
ret

View File

@ -10,9 +10,6 @@ section .text
;----- strlen(char* str) -----;
; return value: amount of characters before EOS was found
strlen:
push rbp
mov rbp, rsp
xor rax, rax
.findEOS:
cmp byte [rdi+rax], 0x0
@ -21,14 +18,10 @@ strlen:
jmp .findEOS
.quit:
leave
ret
;----- strcpy(char* dest, char* src) -----;
; return value: pointer to dest or NULL if nothing was copied (note that EOS is always copied to dest)
strcpy:
push rbp
mov rbp, rsp
mov r11, rdi
xor rax, rax
xor rcx, rcx
@ -49,14 +42,10 @@ strcpy:
mov rax, rdi
.quit:
leave
ret
;----- strlcpy(char* dest, char* src, int size) -----;
; return value: Length of src
strlcpy:
push rbp
mov rbp, rsp
mov r9, rsi
test rdx, rdx
jz .quit
@ -79,29 +68,20 @@ strlcpy:
.quit:
mov rdi, r9
call strlen
leave
ret
;----- strcat(char* dest, char* src) -----;
; return value: pointer to dest
strcat:
push rbp
mov rbp, rsp
mov r8, rdi
call strlen
add rdi, rax
call strcpy
mov rax, r8
leave
ret
;----- strcmp(char* str1, char* str2) -----;
; return value: 0 if both strings are the same, otherwise index in array str1 where strings mismatch
; returns -1 if str2 is longer than str1 and no difference was found before that happens
strcmp:
push rbp
mov rbp, rsp
call strlen
xor r10, r10
.compareLoop:
@ -115,25 +95,21 @@ strcmp:
inc r10
jmp .compareLoop
.0f:
mov rax, -1
cmp byte [rsi], 0x0
jne .quit
xor rax, rax
jmp .quit
mov rax, -1
cmp byte [rsi], 0x0
jne .quit
xor rax, rax
jmp .quit
.mismatch:
mov rax, r10
mov rax, r10
.quit:
leave
ret
;----- strclr(char* str) -----;
; Replaces all characters of a string with \0 untill EOS is found
; returns amount of changed characters
strclr:
push rbp
mov rbp, rsp
call strlen
mov rcx, rax
mov rdx, rax
@ -141,21 +117,14 @@ strclr:
cld
rep stosb
mov rax, rdx
leave
ret
;----- strlclr(char* str, int size) -----;
; Replaces characters of a string with \0, using arg size
; <!> DANGEROUS FUNCTION, it might exceed string boundaries if used incorrectly. Use strclr() instead!
; Function has no return value, rax will be 0 (0x0 stored in rax for opcode stosb)
strlclr:
push rbp
mov rbp, rsp
mov rcx, rsi
xor rax, rax
cld
rep stosb
leave
ret