tolower(), toupper()

This commit is contained in:
2025-06-23 15:47:05 +02:00
parent ed83e83885
commit 3ec591aab4
2 changed files with 56 additions and 0 deletions

View File

@ -5,6 +5,8 @@ section .text
global maxu global maxu
global islower global islower
global isupper global isupper
global tolower
global toupper
;----- min(int a, int b) -----; ;----- min(int a, int b) -----;
; return value: lowest value ; return value: lowest value
@ -86,3 +88,37 @@ isupper:
.quit: .quit:
leave leave
ret 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
xor rax, rax
call isupper
mov rcx, rax
mov al, byte [rdi]
cmp rcx, 1
je .dtl
.dtl:
add al, 32
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
xor rax, rax
call islower
mov rcx, rax
mov al, byte [rdi]
cmp rcx, 1
je .dtu
.dtu:
sub al, 32
leave
ret

View File

@ -7,6 +7,8 @@ extern max
extern maxu extern maxu
extern islower extern islower
extern isupper extern isupper
extern tolower
extern toupper
;console.asm ;console.asm
extern print extern print
extern puts extern puts
@ -41,6 +43,8 @@ section .rodata
msgStrcpy db "strcpy(str1Copy, str1): %s",10,0 msgStrcpy db "strcpy(str1Copy, str1): %s",10,0
msgStrcat db "strcat(str1Copy, str3): %s",10,0 msgStrcat db "strcat(str1Copy, str3): %s",10,0
msgStrlen3 db "strlen(str1Copy): %d",10,0 msgStrlen3 db "strlen(str1Copy): %d",10,0
msgTolower db "tolower(str1[0]): %c",10,0
msgToupper db "toupper(str1[1]): %c",10,0
section .data section .data
section .bss section .bss
str1Copy resb 32 str1Copy resb 32
@ -194,5 +198,21 @@ main:
lea rdi, [rel msgStrlen3] lea rdi, [rel msgStrlen3]
call printf call printf
; TEST: tolower(str1[0])
lea rdi, [rel str1]
call tolower
mov rsi, rax
xor rax, rax
lea rdi, [rel msgTolower]
call printf
; TEST: toupper(str1[1])
lea rdi, [rel str1+1]
call toupper
mov rsi, rax
xor rax, rax
lea rdi, [rel msgToupper]
call printf
leave leave
ret ret