0% found this document useful (0 votes)
44 views8 pages

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

The document contains 3 code exercises: 1) A program that calculates a mathematical expression and verifies the result is correct. 2) A program that reverses a string by pushing characters onto a stack and popping them off. 3) A program that removes vowels from a string by checking each character and skipping vowels.

Uploaded by

Duy Tran Minh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views8 pages

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

The document contains 3 code exercises: 1) A program that calculates a mathematical expression and verifies the result is correct. 2) A program that reverses a string by pushing characters onto a stack and popping them off. 3) A program that removes vowels from a string by checking each character and skipping vowels.

Uploaded by

Duy Tran Minh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

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:
.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

mul $t0, $t0, $t1 # get ab


li $t3, 3
mul $t0, $t0, $t3 # get 3ab
subu $sp, $sp, 4
sw $t0, 0($sp) # push 3ab onto stack

mul $t0, $t1, $t2 # get bc


li $t3, -2
mul $t0, $t0, $t3 # get -2bc
subu $sp, $sp, 4
sw $t0, 0($sp) # push -2bc onto stack

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

lw $t0, 0($sp) # pop 20ac


addu $sp, $sp, 4
addu $t0, $t0, -16 # 20ac - 16

lw $t1, 0($sp) # pop -5a


addu $sp, $sp, 4
addu $t0, $t0, $t1 # -5a + 20ac - 16

lw $t1, 0($sp) # pop -2bc


addu $sp, $sp, 4
addu $t0, $t0, $t1 # -2bc - 5a + 20ac - 16

lw $t1, 0($sp) # pop 3ab


addu $sp, $sp, 4
addu $t0, $t0, $t1 # 3ab -2bc - 5a + 20ac - 16 = -196

li $v0, 4
la $a0, prompt2
syscall

li $v0, 1 # print sum


move $a0, $t0
syscall
li $v0, 10 # exit
syscall

Result: The result calculated by hand should be -196, and the result from the code
above is:

which is therefore correct.


Exercise 2:
.data
prompt1: .asciiz "\nThis program prints the inverse of a string
\nEnter the string: "
prompt2: .asciiz "\nThe result is: "
str: .space 128 # character buffer

.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

subu $sp, $sp, 4 # push the full word


addi $t2, $t2, 1 # set $t2 as counter
sw $t0, 0($sp) # holding the char

addu $t1, 1 # increment the index


j push # loop

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

sb $t0, str($t1) # store at string[$t1]


addu $t1, 1 # increment the index
addi $t2, $t2, -1

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

li $t1, 0 # initiate index


li $t3, 0 # vowel count

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

sub $t2, $t1, $t3


sb $t0, str($t2)
j next

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

li $v0, 10 # exit program


syscall
Result:

You might also like