[AC1] aula03 except adicional exercises

This commit is contained in:
Tiago Garcia 2023-10-03 17:55:46 +01:00
parent 3a1a0a530a
commit 8443c22504
Signed by: TiagoRG
GPG Key ID: DFCD48E3F420DB42
3 changed files with 90 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1,44 @@
.data
str1: .asciiz "Introduza um numero: "
str2: .asciiz "Valor ignorado\n"
str3: .asciiz "A soma dos positivos e: "
.eqv print_string,4
.eqv read_int,5
.eqv print_int,1
.text
.globl main
# Mapa de registos:
# soma: $t0
# valor: $t1
# i: $t2
main: li $t2,0 # i=0
li $t0,0 # soma=0
for: bge $t2,5,endfor # while (i<5) do {
li $v0,print_string
la $a0,str1
syscall # printf("Introduza um numero: ")
li $v0,read_int
syscall # scanf("%d",&valor)
move $t1,$v0 # valor=scanf("%d",&valor)
if: ble $t1,0,else # if (valor<0) then {
add $t0,$t0,$t1 # soma+=valor
addi $t2,$t2,1 # i+=1
j endif
else: li $v0,print_string
la $a0,str2
syscall # printf("Valor ignorado\n")
endif: j for # }
endfor: li $v0,print_string
la $a0,str3
syscall # printf("A soma dos positivos e: %d\n",soma")
li $v0,print_int
move $a0,$t0
syscall # printf("%d\n",soma)
jr $ra # }

View File

@ -0,0 +1,46 @@
.data
str1: .asciiz "Introduza um numero: "
str2: .asciiz "O numero em binario e: "
.eqv print_string, 4
.eqv read_int, 5
.eqv print_int, 1
.text
.globl main
# Mapa de registos:
# valor: $t0
# bit: $t1
# i: $t2
main: la $a0, str1
li $v0, print_string
syscall # Imprime a mensagem "Introduza um numero: "
li $v0, read_int
syscall # Le o numero introduzido pelo utilizador
move $t0, $v0 # Guarda o numero introduzido em $t0
li $v0, print_string
la $a0, str2
syscall # Imprime a mensagem "O numero em binario e: "
li $t2, 0 # Inicializa o contador i a 0
for: bge $t2, 32, endfor # Se i >= 32, termina o ciclo
li $t3,0x80000000
and $t1, $t0, $t3 # bit = valor & 0x80000000
if: beq $t1, 0, else # Se bit == 0, salta para else
li $a0, 1 # Caso contrario, imprime 1
j endif
else: li $a0, 0 # Imprime 0
endif: li $v0, print_int
syscall # Imprime o valor de a0
sll $t0, $t0, 1 # valor = valor << 1
addi $t2,$t2,1 # i+=1
j for
endfor: jr $ra # Salta para o endereco de retorno