From 57368425d82fbccd8487e1e3aad5e6af9a97cd10 Mon Sep 17 00:00:00 2001 From: TiagoRG Date: Tue, 14 Nov 2023 11:27:35 +0000 Subject: [PATCH] [AC1] Finish aula03 Signed-off-by: TiagoRG --- 2ano/1semestre/ac1/aula03/ex1-addicional.asm | 51 ++++++++++++++++++ 2ano/1semestre/ac1/aula03/ex2-addicional.asm | 55 ++++++++++++++++++++ 2ano/1semestre/ac1/aula03/ex2-addicional.md | 13 +++++ 3 files changed, 119 insertions(+) create mode 100644 2ano/1semestre/ac1/aula03/ex1-addicional.asm create mode 100644 2ano/1semestre/ac1/aula03/ex2-addicional.asm create mode 100644 2ano/1semestre/ac1/aula03/ex2-addicional.md diff --git a/2ano/1semestre/ac1/aula03/ex1-addicional.asm b/2ano/1semestre/ac1/aula03/ex1-addicional.asm new file mode 100644 index 0000000..25255a4 --- /dev/null +++ b/2ano/1semestre/ac1/aula03/ex1-addicional.asm @@ -0,0 +1,51 @@ + .data +str1: .asciiz "Introduza um numero: " +str2: .asciiz "\nValor em codigo gray: " +str3: .asciiz "\nValor em binario: " + .eqv print_int, 1 + .eqv print_string, 4 + .eqv read_int, 5 + .text + .globl main + +# Mapa de registos +# gray: $t0 +# bin: $t1 +# mask: $t2 + +main: li $v0, print_string + la $a0, str1 + syscall # print_string("Introduza um numero: "); + + li $v0, read_int + syscall # read_int(); + move $t0, $v0 # gray = read_int(); + + srl $t2, $t0, 1 # mask = gray >> 1; + move $t1, $t0 # bin = gray; + +while: beq $t2, 0, done # while (mask != 0) { + + xor $t1, $t1, $t2 # bin = bin ^ mask; + srl $t2, $t2, 1 # mask = mask >> 1; + + j while # } + +done: + li $v0, print_string + la $a0, str2 + syscall # print_string("\nValor em codigo gray: "); + + li $v0, print_int + move $a0, $t0 + syscall # print_int(gray); + + li $v0, print_string + la $a0, str3 + syscall # print_string("\nValor em binario: "); + + li $v0, print_int + move $a0, $t1 + syscall # print_int(bin); + + jr $ra diff --git a/2ano/1semestre/ac1/aula03/ex2-addicional.asm b/2ano/1semestre/ac1/aula03/ex2-addicional.asm new file mode 100644 index 0000000..ffe0143 --- /dev/null +++ b/2ano/1semestre/ac1/aula03/ex2-addicional.asm @@ -0,0 +1,55 @@ + .data +str1: .asciiz "Introduza dois numeros: " +str2: .asciiz "Resultado: " + .eqv print_int, 1 + .eqv print_string, 4 + .eqv read_int, 5 + .text + .globl main + +# Mapa de registos +# $t0: res +# $t1: mdor +# $t2: mdo +# $t3: i + +main: la $a0, str1 + li $v0, print_string + syscall # print_string("Introduza dois numeros: "); + + li $v0, read_int + syscall # read_int() + move $t1, $v0 # mdor = read_int(); + andi $t1, $t1, 0x0000FFFF # mdor = mdor & 0x0000FFFF; + + li $v0, read_int + syscall # read_int() + move $t2, $v0 # mdo = read_int(); + andi $t2, $t2, 0x0000FFFF # mdo = mdo & 0x0000FFFF; + + li $t0, 0 # res = 0; + li $t3, 0 # i = 0; + +while: beq $t1, 0, endw # while (mdor != 0) + bge $t3, 16, endw # while (i < 4) { + +if: andi $t4, $t1, 0x00000001 # $t4 = mdor & 0x00000001; + beq $t4, 0, else + + add $t0, $t0, $t2 # res = res + mdo; + +else: sll $t2, $t2, 1 # mdo = mdo << 1; + srl $t1, $t1, 1 # mdor = mdor >> 1; + + addi $t3, $t3, 1 # i++; + j while # } + +endw: la $a0, str2 + li $v0, print_string + syscall # print_string("Resultado: "); + + li $v0, print_int + move $a0, $t0 # print_int(res); + syscall + + jr $ra # return diff --git a/2ano/1semestre/ac1/aula03/ex2-addicional.md b/2ano/1semestre/ac1/aula03/ex2-addicional.md new file mode 100644 index 0000000..865fb97 --- /dev/null +++ b/2ano/1semestre/ac1/aula03/ex2-addicional.md @@ -0,0 +1,13 @@ +# Alinea b) + +| mdor | mdo | i | res | | +|:----:|:----:|:-:|:----:|:-------------------| +| 0x0B | 0x06 | 0 | 0x00 | Valores iniciais | +| 0x05 | 0x0C | 1 | 0x06 | Fim da 1ª iteração | +| 0x02 | 0x18 | 2 | 0x12 | Fim da 2ª iteração | +| 0x01 | 0x30 | 3 | 0x12 | Fim da 3ª iteração | +| 0x00 | 0x60 | 4 | 0x42 | Fim da 4ª iteração | + +# Alinea c) + +Tudo o que é necessário fazer é alterar a condição do `if` para comparar o `i` a 16 em vez de 4 ([linha 34 no código de assembly](https://github.com/TiagoRG/uaveiro-leci/tree/master/2ano/1semestre/ac1/aula03/ex2-addicional.asm))