From b0f6d15cacee84991223e6e2962b3a4857d9ef6e Mon Sep 17 00:00:00 2001 From: TiagoRG Date: Tue, 5 Mar 2024 01:17:59 +0000 Subject: [PATCH] [AC2] Aula01 Addicionals Signed-off-by: TiagoRG --- 2ano/2semestre/ac2/aula01/addicional-1.c | 39 ++++ 2ano/2semestre/ac2/aula01/addicional-1.s | 107 +++++++++++ 2ano/2semestre/ac2/aula01/addicional-2.c | 68 +++++++ 2ano/2semestre/ac2/aula01/addicional-2.s | 223 +++++++++++++++++++++++ 4 files changed, 437 insertions(+) create mode 100644 2ano/2semestre/ac2/aula01/addicional-1.c create mode 100644 2ano/2semestre/ac2/aula01/addicional-1.s create mode 100644 2ano/2semestre/ac2/aula01/addicional-2.c create mode 100644 2ano/2semestre/ac2/aula01/addicional-2.s diff --git a/2ano/2semestre/ac2/aula01/addicional-1.c b/2ano/2semestre/ac2/aula01/addicional-1.c new file mode 100644 index 0000000..3505d61 --- /dev/null +++ b/2ano/2semestre/ac2/aula01/addicional-1.c @@ -0,0 +1,39 @@ +#include + +#define UP 1 +#define DOWN 0 + +void wait(int); + +int main(void) { + int state = 0; + int cnt = 0; + char c; + do { + putChar('\r'); // Carriage return character + printInt(cnt, 10 | 3 << 16); // 0x0003000A: decimal w/ 3 digits + putChar('\t'); // Tab character + printInt(cnt, 2 | 8 << 16); // 0x00080002: binary w/ 8 bits + + wait(5); // wait 0.5s + + c = inkey(); + if (c == '+') + state = UP; + if (c == '-') + state = DOWN; + if (c == 'S') + while (inkey() != 'R'); + + if (state == UP) + cnt = (cnt + 1) & 0xFF; // Up counter MOD 256 + else + cnt = (cnt - 1) & 0xFF; // Down counter MOD 256 + } while (c != 'q'); + return 0; +} + +void wait(int ts) { + int i; + for (i = 0; i < 515000 * ts; i++); // wait approximately ts/10 seconds +} diff --git a/2ano/2semestre/ac2/aula01/addicional-1.s b/2ano/2semestre/ac2/aula01/addicional-1.s new file mode 100644 index 0000000..161085a --- /dev/null +++ b/2ano/2semestre/ac2/aula01/addicional-1.s @@ -0,0 +1,107 @@ + .equ INKEY, 1 + .equ GETCHAR, 2 + .equ PUTCHAR, 3 + .equ READ_INT, 4 + .equ READ_INT10, 5 + .equ PRINT_INT, 6 + .equ PRINT_INT10, 7 + .equ PRINT_STR, 8 + .equ READ_STR, 9 + .equ EXIT, 10 + .equ READ_CORE_TIMER, 11 + .equ RESET_CORE_TIMER, 12 + + .equ UP, 1 + .equ DOWN, 0 + + .data + .text + .globl main + +# Mapa de registos +# $t0 → state +# $t1 → cnt +# $t2 → c + +main: + li $t0, 0 + li $t1, 0 + +do: + li $a0, '\r' + li $v0, PUTCHAR + syscall + + move $a0, $t1 + li $a1, 3 + sll $a1, $a1, 16 + ori $a1, $a1, 10 + li $v0, PRINT_INT + syscall + + li $a0, '\t' + li $v0, PUTCHAR + syscall + + move $a0, $t1 + li $a1, 8 + sll $a1, $a1, 16 + ori $a1, $a1, 2 + li $v0, PRINT_INT + syscall + + addi $sp, $sp, -16 + sw $ra, 0($sp) + sw $t0, 4($sp) + sw $t1, 8($sp) + sw $t2, 12($sp) + li $a0, 5 + jal wait + lw $ra, 0($sp) + lw $t0, 4($sp) + lw $t1, 8($sp) + lw $t2, 12($sp) + addi $sp, $sp, 16 + + li $v0, INKEY + syscall + move $t2, $v0 + + beq $t2, '+', if_plus + beq $t2, '-', if_minus + beq $t2, 'S', if_stop + j if +if_plus: + li $t0, UP + j if +if_minus: + li $t0, DOWN + j if +if_stop: + li $v0, INKEY + syscall + move $t2, $v0 + bne $t2, 'R', if_stop + +if: + bne $t0, UP, else + addi $t1, $t1, 1 + andi $t1, $t1, 0xFF + j endif +else: + addi $t1, $t1, -1 + andi $t1, $t1, 0xFF +endif: + bne $t2, 'q', do + jr $ra + + +wait: + li $t0, 0 + mul $t1, $a0, 515000 +wait_loop: + bge $t0, $t1, wait_return + addi $t0, $t0, 1 + j wait_loop +wait_return: + jr $ra diff --git a/2ano/2semestre/ac2/aula01/addicional-2.c b/2ano/2semestre/ac2/aula01/addicional-2.c new file mode 100644 index 0000000..3dfd8af --- /dev/null +++ b/2ano/2semestre/ac2/aula01/addicional-2.c @@ -0,0 +1,68 @@ +#include + +#define SIZE 20 + +char *strcat(char *, char *); +char *strcpy(char *, char *); +int strlen(char *); +int strcmp(char *str1, char *str2); + +int main(void) { + static char str1[SIZE + 1]; + static char str2[SIZE + 1]; + static char str3[2 * SIZE + 1]; + + printStr("Introduza 2 strings:\n"); + readStr(str1, SIZE); + putChar('\n'); + readStr(str2, SIZE); + putChar('\n'); + printStr("Resultados:\n"); + printStr("Str1 len: "); + printInt(strlen(str1), 10); + putChar('\n'); + printStr("Str2 len: "); + printInt(strlen(str2), 10); + putChar('\n'); + strcpy(str3, str1); + printStr("Str3 + Str2: "); + printStr(strcat(str3, str2)); + putChar('\n'); + printStr("Str1 == Str2: "); + printInt10(strcmp(str1, str2)); + putChar('\n'); + return 0; +} +// Returns the length of string "str" (excluding the null character) +int strlen(char *str) { + int len; + for (len = 0; *str != '\0'; len++, str++); + return len; +} +// Copy the string pointed by "src" (including the null character) to +// destination (pointed by "dst") +char *strcpy(char *dst, char *src) { + char *p = dst; + for (; (*dst = *src) != '\0'; dst++, src++); + return p; +} +// Concatenates "dst" and "src" strings +// The result is stored in the "dst" string +char *strcat(char *dst, char *src) { + char *p = dst; + for (; *dst != '\0'; dst++); + strcpy(dst, src); + return p; +} + +// Compares two strings, character by character +// Returned value is: +// < 0 string "str1" is "less than" string "str2" (first +// non-matching character in str1 is lower, in ASCII, than that of str2) +// = 0 string "str1" is equal to string "str2" +// > 0 string "str1" is "greater than" string "str2" (first +// non-matching character in str1 is greater, in ASCII, than that of str2) +int strcmp(char *str1, char *str2) { + for (; (*str1 == *str2) && (*str1 != '\0'); str1++, str2++); + return (*str1 - *str2); +} diff --git a/2ano/2semestre/ac2/aula01/addicional-2.s b/2ano/2semestre/ac2/aula01/addicional-2.s new file mode 100644 index 0000000..78ac89f --- /dev/null +++ b/2ano/2semestre/ac2/aula01/addicional-2.s @@ -0,0 +1,223 @@ + .equ INKEY, 1 + .equ GETCHAR, 2 + .equ PUTCHAR, 3 + .equ READ_INT, 4 + .equ READ_INT10, 5 + .equ PRINT_INT, 6 + .equ PRINT_INT10, 7 + .equ PRINT_STR, 8 + .equ READ_STR, 9 + .equ EXIT, 10 + .equ READ_CORE_TIMER, 11 + .equ RESET_CORE_TIMER, 12 + + .equ SIZE, 20 + + .data + +input: .asciiz "Introduza 2 strings:\n" +r_str: .asciiz "Resultados:\n" +res1: .asciiz "Str1 len: " +res2: .asciiz "Str2 len: " +res3: .asciiz "Str3 + Str2: " +res4: .asciiz "Str1 == Str2: " + +str1: .space 21 +str2: .space 21 +str3: .space 41 + + .text + .globl main + +main: + move $s0, $ra # Save $ra + + la $a0, input + li $v0, PRINT_STR + syscall + + la $a0, str1 + li $a1, SIZE + li $v0, READ_STR + syscall + + li $a0, '\n' + li $v0, PUTCHAR + syscall + + la $a0, str2 + li $a1, SIZE + li $v0, READ_STR + syscall + + li $a0, '\n' + li $v0, PUTCHAR + syscall + + la $a0, r_str + li $v0, PRINT_STR + syscall + + la $a0, res1 + li $v0, PRINT_STR + syscall + + la $a0, str1 + jal strlen + move $a0, $v0 + li $a1, 10 + li $v0, PRINT_INT + syscall + + li $a0, '\n' + li $v0, PUTCHAR + syscall + + la $a0, res2 + li $v0, PRINT_STR + syscall + + la $a0, str2 + jal strlen + move $a0, $v0 + li $a1, 10 + li $v0, PRINT_INT + syscall + + li $a0, '\n' + li $v0, PUTCHAR + syscall + + la $a0, str3 + la $a1, str1 + jal strcpy + + la $a0, res3 + li $v0, PRINT_STR + syscall + + la $a0, str3 + la $a1, str2 + jal strcat + move $a0, $v0 + li $v0, PRINT_STR + syscall + + li $a0, '\n' + li $v0, PUTCHAR + syscall + + la $a0, res4 + li $v0, PRINT_STR + syscall + + la $a0, str1 + la $a1, str2 + jal strcmp + move $a0, $v0 + li $v0, PRINT_INT10 + syscall + + li $a0, '\n' + li $v0, PUTCHAR + syscall + + jr $s0 + + + +#---------------------------------------------- +# STRLEN +#---------------------------------------------- +# Mapa de registos +# $a0 → str +# $t0 → len +# $t1 → *str +strlen: + li $t0, 0 +strlen_loop: + lb $t1, 0($a0) + beq $t1, 0, strlen_return + addiu $t0, $t0, 1 + addiu $a0, $a0, 1 + j strlen_loop +strlen_return: + move $v0, $t0 + jr $ra +#---------------------------------------------- + +#---------------------------------------------- +# STRCPY +#---------------------------------------------- +# Mapa de registos +# $a0 → dst +# $a1 → src +# $t0 → p +# $t1 → *src +strcpy: + move $t0, $a0 +strcpy_loop: + lb $t1, 0($a1) + sb $t1, 0($a0) + beq $t1, 0, strcpy_return + addiu $a0, $a0, 1 + addiu $a1, $a1, 1 + j strcpy_loop +strcpy_return: + move $v0, $t0 + jr $ra +#---------------------------------------------- + +#---------------------------------------------- +# STRCAT +#---------------------------------------------- +# Mapa de registos +# $a0 → dst +# $a1 → src +# $t0 → p +# $t1 → *dst +strcat: + move $t0, $a0 +strcat_loop: + lb $t1, 0($a0) + beq $t1, 0, strcat_return + addiu $a0, $a0, 1 + j strcat_loop +strcat_return: + addiu $sp, $sp, -20 + sw $ra, 0($sp) + sw $a0, 4($sp) + sw $a1, 8($sp) + sw $t0, 12($sp) + sw $t1, 16($sp) + jal strcpy + lw $ra, 0($sp) + lw $a0, 4($sp) + lw $a1, 8($sp) + lw $t0, 12($sp) + lw $t1, 16($sp) + addiu $sp, $sp, 20 + move $v0, $t0 + jr $ra +#---------------------------------------------- + +#---------------------------------------------- +# STRCMP +#---------------------------------------------- +# Mapa de registos +# $a0 → str1 +# $a1 → str2 +# $t0 → *str1 +# $t1 → *str2 +strcmp: + lb $t0, 0($a0) + lb $t1, 0($a1) + bne $t0, $t1, strcmp_return + beq $t0, 0, strcmp_return + addiu $a0, $a0, 1 + addiu $a1, $a1, 1 + j strcmp +strcmp_return: + sub $v0, $t0, $t1 + jr $ra +#----------------------------------------------