Computer Architecture Lab - Course: It089Iu Group 1 - Lab 1 Laboratory Session 6: Stack Name: Trần Minh Duy Student's ID: ITITIU18230 Exercise 1: Code
Computer Architecture Lab - Course: It089Iu Group 1 - Lab 1 Laboratory Session 6: Stack Name: Trần Minh Duy Student's ID: ITITIU18230 Exercise 1: Code
GROUP 1 – LAB 1
LABORATORY SESSION 6: Stack
Name: Trần Minh Duy
Student’s ID: ITITIU18230
Exercise 1:
Code:
.data
prompt1: .asciiz "This program calculates 3ab − 2bc − 5a + 20ac − 16\n(a = 0, b =
9, c = 10)"
prompt2: .asciiz "\nThe result is: "
a: .word 0 # set a = 0
bb: .word 9 # set b = 9
cc: .word 10 # set c = 10
.text
.globl main
main:
li $v0, 4
la $a0, prompt1
syscall
lw $t0, a # get a
lw $t1, bb # get b
lw $t2, cc # get c
lw $t0, a # get a
li $t1, -5
mul $t0, $t0, $t1 # get -5a
subu $sp, $sp, 4
sw $t0,0($sp) # push -5a onto stack
lw $t0, a # get a
mul $t0, $t0, $t2 # get ac
li $t1, 20
mul $t0, $t0, $t1 # get 20ac
subu $sp, $sp, 4
sw $t0, 0($sp) # push 20ac onto stack
li $v0, 4
la $a0, prompt2
syscall
Result: The result calculated by hand should be -196, and the result from the code
above is:
.text
.globl main
main:
li $v0, 4
la $a0, prompt1
syscall
li $v0, 8 # service code for read string
la $a0, str # address of buffer
li $a1, 128 # buffer length
syscall
li $t1, 0 # index of first char in str buffer
push:
lbu $t0, str($t1) # get current char into a full word
beqz $t0, strend # null byte: end of string
strend:
li $t1, 0 # index of 1st byte of str buffer
j pop
pop:
lw $t0,0($sp) # pop a char off the stack
addu $sp, $sp, 4
beqz $t2, done # null means empty stack
j pop # loop
done:
li $v0, 4
la $a0, prompt2
syscall
li $v0, 4
la $a0, str # address of string
syscall
li $v0,10 # exit
syscall
Result:
Exercise 3:
Code:
.data
prompt1: .asciiz "\nThis program removes vowels from a string
\nEnter the string: "
prompt2: .asciiz "\nThe result is: "
str: .space 128
.text
.globl main
main:
li $v0, 4
la $a0, prompt1
syscall
li $v0, 8
la $a0, str
li $a1, 128
syscall
poploop:
lb $t0, str($t1)
# check if vowel
li $t2, 'a' #a
beq $t0, $t2, vowel
li $t2, 'A' #A
beq $t0, $t2, vowel
li $t2, 'e' #e
beq $t0, $t2, vowel
li $t2, 'E' #E
beq $t0, $t2, vowel
li $t2, 'i' #i
beq $t0, $t2, vowel
li $t2, 'I' #I
beq $t0, $t2, vowel
li $t2, 'o' #o
beq $t0, $t2, vowel
li $t2, 'O' #O
beq $t0, $t2, vowel
li $t2, 'u' #u
beq $t0, $t2, vowel
li $t2, 'U' #U
beq $t0, $t2, vowel
vowel:
addi $t3, $t3, 1
next:
addi $t1, $t1, 1
beqz $t0, done # exit if reach null
j poploop
done:
li $v0, 4
la $a0, str
syscall