From 34d7fee8dcec1e4e67e77f856fbc5126d95575a4 Mon Sep 17 00:00:00 2001 From: Kwarde Date: Thu, 10 Jul 2025 11:42:35 +0200 Subject: [PATCH] Adds tolower(), toupper() --- src/core.asm | 36 ++++++++++++++++ src/tests.asm | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) diff --git a/src/core.asm b/src/core.asm index 5339070..e4a22ba 100644 --- a/src/core.asm +++ b/src/core.asm @@ -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 diff --git a/src/tests.asm b/src/tests.asm index d2c6d65..7b8ea43 100644 --- a/src/tests.asm +++ b/src/tests.asm @@ -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() ;---