Adds tolower(), toupper()

This commit is contained in:
2025-07-10 11:42:35 +02:00
parent 3951e50ab0
commit 34d7fee8dc
2 changed files with 150 additions and 0 deletions

View File

@@ -4,6 +4,8 @@ section .text
global exit
global islower
global isupper
global tolower
global toupper
;----- exit(exit_code) -----;
; Exits the program with given exit code
@@ -47,3 +49,37 @@ isupper:
mov rax, 1
.quit:
ret
;----- tolower(c) -----;
; Converts given ASCII character to lowercase if it is uppercase
; Return value: Converted lowercase ASCII character or the original input if it wasn't uppercase
; Used registers:
; rax* (ret)
; rdi (arg)
tolower:
call isupper
test al, al
jnz .makelow
mov rax, rdi
ret
.makelow:
mov rax, rdi
add rax, 32
ret
;----- toupper(c) -----;
; Converts given ASCII character to uppercase if it is lowercase
; Return value: Converted uppercase ASCII character or the original input if it wasn't lowercase
; Used registers:
; rax* (ret)
; rdi (arg)
toupper:
call islower
test al, al
jnz .makeupp
mov rax, rdi
ret
.makeupp:
mov rax, rdi
sub rax, 32
ret

View File

@@ -4,6 +4,8 @@
extern exit
extern islower
extern isupper
extern tolower
extern toupper
; console.asm
extern print
extern puts
@@ -31,6 +33,8 @@ section .rodata
TEST_strclr equ 1
TEST_islower equ 1
TEST_isupper equ 1
TEST_tolower equ 1
TEST_toupper equ 1
TEST_strcmp equ 1
str1 db "Hello, world!",EOS
@@ -114,6 +118,20 @@ section .rodata
isupper4 db TAB,"isupper('D'): %d",NL,EOS
isupper5 db TAB,"isupper('!'): %d",NL,EOS
; tolower()
msgTolower db NL,"TEST tolower()",NL,EOS
tolower1 db TAB,"tolower(str1[0]): %c",NL,EOS
tolower2 db TAB,"tolower(str1[1]): %c",NL,EOS
tolower3 db TAB,"tolower('!'): %c",NL,EOS
tolower4 db TAB,"tolower() -- entire str4: %s",NL,EOS
; toupper()
msgToupper db NL,"TEST tolower()",NL,EOS
toupper1 db TAB,"toupper(str1[0]): %c",NL,EOS
toupper2 db TAB,"toupper(str1[1]): %c",NL,EOS
toupper3 db TAB,"toupper('!'): %c",NL,EOS
toupper4 db TAB,"toupper() -- entire str4: %s",NL,EOS
; strcmp()
msgStrcmp db NL,"TEST strcmp()",NL,EOS
strcmp1 db TAB,"strcmp(str1, str1): %d",NL,EOS
@@ -512,6 +530,102 @@ _start:
call printf
%endif
;---
;--- tolower()
;---
%if TEST_tolower
lea rdi, [rel msgTolower]
call print
; TEST 1
mov dil, byte [str1]
call tolower
lea rdi, [rel tolower1]
mov rsi, rax
call printf
; TEST 2
mov dil, byte [str1 + 1]
call tolower
lea rdi, [rel tolower2]
mov rsi, rax
call printf
; TEST 3
mov dil, '!'
call tolower
lea rdi, [rel tolower3]
mov rsi, rax
call printf
; TEST 4
lea rdi, [rel strBuff1]
lea rsi, [rel str4]
mov rdx, 32
call strcpy
mov rdi, rax
call strlen
mov rcx, rax
lea rsi, [rel strBuff1]
.tolowerLoop:
mov dil, byte [rsi]
call tolower
mov byte [rsi], al
inc rsi
loop .tolowerLoop
lea rdi, [rel tolower4]
lea rsi, [rel strBuff1]
call printf
%endif
;---
;--- toupper()
;---
%if TEST_toupper
lea rdi, [rel msgToupper]
call print
; TEST 1
mov dil, byte [str1]
call toupper
lea rdi, [rel toupper1]
mov rsi, rax
call printf
; TEST 2
mov dil, byte [str1 + 1]
call toupper
lea rdi, [rel toupper2]
mov rsi, rax
call printf
; TEST 3
mov dil, '!'
call toupper
lea rdi, [rel toupper3]
mov rsi, rax
call printf
; TEST 4
lea rdi, [rel strBuff1]
lea rsi, [rel str4]
mov rdx, 32
call strcpy
mov rdi, rax
call strlen
mov rcx, rax
lea rsi, [rel strBuff1]
.toupperLoop:
mov dil, byte [rsi]
call toupper
mov byte [rsi], al
inc rsi
loop .toupperLoop
lea rdi, [rel toupper4]
lea rsi, [rel strBuff1]
call printf
%endif
;---
;--- strcmp()
;---