From e0bd8bcdbd24fae18d7b7b5e639cddc4b006a427 Mon Sep 17 00:00:00 2001 From: Kwarde Date: Thu, 10 Jul 2025 11:58:29 +0200 Subject: [PATCH] Adds min(), minu(), max(), maxu() --- src/core.asm | 56 +++++++++++++++ src/tests.asm | 187 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 243 insertions(+) diff --git a/src/core.asm b/src/core.asm index e4a22ba..bcc78ee 100644 --- a/src/core.asm +++ b/src/core.asm @@ -6,6 +6,10 @@ section .text global isupper global tolower global toupper + global min + global minu + global max + global maxu ;----- exit(exit_code) -----; ; Exits the program with given exit code @@ -83,3 +87,55 @@ toupper: mov rax, rdi sub rax, 32 ret + +;----- min(num1, num2) -----; +; Takes two signed integers, returns lowest +; Return value: Lowest signed integer +; Used registers: +; rax* (ret) lowest signed int +; rdi (arg) num1 +; rsi (arg) num2 +min: + mov rax, rsi + cmp rdi, rsi + cmovl rax, rdi + ret + +;----- minu(num1, num2) -----; +; Takes two unsigned integers, returns lowest +; Return value: Lowest unsigned int +; Used registers: +; rax* (ret) lowest unsigned int +; rdi (arg) num1 +; rsi (arg) num2 +minu: + mov rax, rsi + cmp rdi, rsi + cmovb rax, rdi + ret + +;----- max(num1, num2) -----; +; Takes two signed integers, returns highest +; Return value: Highest signed int +; Used registers: +; rax* (ret) highest signed int +; rdi (arg) num1 +; rsi (arg) num2 +max: + mov rax, rsi + cmp rdi, rsi + cmovg rax, rdi + ret + +;----- maxu(num1, num2) -----; +; Takes two unsigned integers, returns highest +; Return value: Highest unsigned int +; Used registers: +; rax* (ret) highest unsigned int +; rdi (arg) num1 +; rsi (arg) num2 +maxu: + mov rax, rsi + cmp rdi, rsi + cmova rax, rdi + ret diff --git a/src/tests.asm b/src/tests.asm index 7b8ea43..74ffd80 100644 --- a/src/tests.asm +++ b/src/tests.asm @@ -6,6 +6,10 @@ extern islower extern isupper extern tolower extern toupper +extern min +extern minu +extern max +extern maxu ; console.asm extern print extern puts @@ -36,6 +40,8 @@ section .rodata TEST_tolower equ 1 TEST_toupper equ 1 TEST_strcmp equ 1 + TEST_min equ 1 ;includes minu + TEST_max equ 1 ;includes maxu str1 db "Hello, world!",EOS str2 db "Hello, World!",EOS @@ -141,6 +147,27 @@ section .rodata strcmp5 db TAB,"- strclr(strBuff1, 32)",NL,TAB,"strcmp(strBuff1, str1): %d",NL,TAB,"strcmp(str1, strBuff1): %d",NL,EOS strcmp6 db TAB,"strcmp(str1, str5): %d",NL,EOS + ; min() / minu() + msgMin db NL,"TEST min(), minu()",NL,EOS + min1 db TAB,"min(15, 30):",NL,TAB,TAB,"%d (%x)",NL,EOS + min2 db TAB,"min(-15, -30):",NL,TAB,TAB,"%d (%x)",NL,EOS + min3 db TAB,"min(-1, 0):",NL,TAB,TAB,"%d (%x)",NL,EOS + min4 db TAB,"min(0xFFFFFFFF, -4294967295):",NL,TAB,TAB,"%d (%x)",NL,EOS + minu1 db TAB,"minu(15, 30):",NL,TAB,TAB,"%d (%x)",NL,EOS + minu2 db TAB,"minu(-15, -30):",NL,TAB,TAB,"%d (%x)",NL,EOS + minu3 db TAB,"minu(-1, 0):",NL,TAB,TAB,"%d (%x)",NL,EOS + minu4 db TAB,"minu(0xFFFFFFFF, -4294967295):",NL,TAB,TAB,"%d (%x)",NL,EOS + + ; max() / maxu() + msgMax db NL,"TEST max(), maxu()",NL,EOS + max1 db TAB,"max(15, 30):",NL,TAB,TAB,"%d (%x)",NL,EOS + max2 db TAB,"max(-15, -30):",NL,TAB,TAB,"%d (%x)",NL,EOS + max3 db TAB,"max(-1, 0):",NL,TAB,TAB,"%d (%x)",NL,EOS + max4 db TAB,"max(0xFFFFFFFF, -4294967295):",NL,TAB,TAB,"%d (%x)",NL,EOS + maxu1 db TAB,"maxu(15, 30):",NL,TAB,TAB,"%d (%x)",NL,EOS + maxu2 db TAB,"maxu(-15, -30):",NL,TAB,TAB,"%d (%x)",NL,EOS + maxu3 db TAB,"maxu(-1, 0):",NL,TAB,TAB,"%d (%x)",NL,EOS + maxu4 db TAB,"maxu(0xFFFFFFFF, -4294967295):",NL,TAB,TAB,"%d (%x)",NL,EOS section .data section .bss @@ -698,6 +725,166 @@ _start: call printf %endif +;--- +;--- min() / minu() +;--- +%if TEST_min + lea rdi, [rel msgMin] + call print + + ; TEST 1 + mov rdi, 15 + mov rsi, 30 + call min + lea rdi, [rel min1] + mov rsi, rax + mov rdx, rax + call printf + + ; TEST 2 + mov rdi, -15 + mov rsi, -30 + call min + lea rdi, [rel min2] + mov rsi, rax + mov rdx, rax + call printf + + ; TEST 3 + mov rdi, -1 + xor rsi, rsi + call min + lea rdi, [rel min3] + mov rsi, rax + mov rdx, rax + call printf + + ; TEST 4 + mov rdi, 0xFFFFFFFF + mov rsi, -4294967295 + call min + lea rdi, [rel min4] + mov rsi, rax + mov rdx, rax + call printf + + ; TEST 1 + mov rdi, 15 + mov rsi, 30 + call minu + lea rdi, [rel minu1] + mov rsi, rax + mov rdx, rax + call printf + + ; TEST 2 + mov rdi, -15 + mov rsi, -30 + call minu + lea rdi, [rel minu2] + mov rsi, rax + mov rdx, rax + call printf + + ; TEST 3 + mov rdi, -1 + xor rsi, rsi + call minu + lea rdi, [rel minu3] + mov rsi, rax + mov rdx, rax + call printf + + ; TEST 4 + mov rdi, 0xFFFFFFFF + mov rsi, -4294967295 + call minu + lea rdi, [rel minu4] + mov rsi, rax + mov rdx, rax + call printf +%endif + +;--- +;--- max() / maxu() +;--- +%if TEST_max + lea rdi, [rel msgMin] + call print + + ; TEST 1 + mov rdi, 15 + mov rsi, 30 + call max + lea rdi, [rel max1] + mov rsi, rax + mov rdx, rax + call printf + + ; TEST 2 + mov rdi, -15 + mov rsi, -30 + call max + lea rdi, [rel max2] + mov rsi, rax + mov rdx, rax + call printf + + ; TEST 3 + mov rdi, -1 + xor rsi, rsi + call max + lea rdi, [rel max3] + mov rsi, rax + mov rdx, rax + call printf + + ; TEST 4 + mov rdi, 0xFFFFFFFF + mov rsi, -4294967295 + call max + lea rdi, [rel max4] + mov rsi, rax + mov rdx, rax + call printf + + ; TEST 1 + mov rdi, 15 + mov rsi, 30 + call maxu + lea rdi, [rel maxu1] + mov rsi, rax + mov rdx, rax + call printf + + ; TEST 2 + mov rdi, -15 + mov rsi, -30 + call maxu + lea rdi, [rel maxu2] + mov rsi, rax + mov rdx, rax + call printf + + ; TEST 3 + mov rdi, -1 + xor rsi, rsi + call maxu + lea rdi, [rel maxu3] + mov rsi, rax + mov rdx, rax + call printf + + ; TEST 4 + mov rdi, 0xFFFFFFFF + mov rsi, -4294967295 + call maxu + lea rdi, [rel maxu4] + mov rsi, rax + mov rdx, rax + call printf +%endif + ;--- ;--- exit() ;---