Professional Documents
Culture Documents
LAB # 12
OBJECTIVE
THEORY
TRANSLATION OF A “FOR LOOP” CONTROL STRUCTURE
Obviously a “for loop” control structure is very useful. Let us suppose that a
programmer initially developed an algorithm containing the following pseudocode. In
one sentence, can you describe what this algorithm accomplishes?
$a0 = 0;
For ( $t0 =10; $t0 > 0; $t0 = $t0 -1)
do{$a0 = $a0 + $t0}
li $a0, 0 # $a0 = 0
li $t0, 10 # Initialize loop counter to 10
loop:
add $a0, $a0, $t0
addi $t0, $t0, -1 # Decrement loop counter
bgtz $t0, loop # If ($t0 > 0) Branch to loop
PROGRAM#1:
Write a program in MIPS assembly language that translateFOR LOOP control
structure.
################## DATA SEGMENT ###################
.data
counter: .asciiz"\n value of count, $t0: "
total: .asciiz"value of sum, $a0: "
tab: .asciiz"\t"
################## TEXT SEGMENT ###################
.text
.globl main
main:
li $a2,0 # $a2=0
li $t0,10 # initiallize loop variable counter $t0=10
loop:
add $a2,$a2,$t0
end:
li $v0,10
syscall
CE-207 Computer Organization & Architecture SSUET/QR/114
OUTPUT:
CE-207 Computer Organization & Architecture SSUET/QR/114
LAB TASKS:
1. Take any integer from user. If it’s positive, print the integer. Otherwise,
program should be closed. Repeat the task, the no. of times of the last digit of
your roll no.
SOURCE CODE:
.data
input: .asciiz "\nPLease Enter Your Integer : "
line: .asciiz "\n"
answer: .asciiz "\nThe number is positve "
.text
.globl main
main:
addi $s0 ,$0, 3
la $a0,line
li $v0,4
syscall
top:
beq $s0, $v0,negative
addi $s0, $s0,-1
la $a0,input
li $v0,4
syscall
li $v0 ,5
syscall
move $t0,$v0
bgtz $t0,positive
bltz $t0 , negative
positive:
la $a0,answer
li $v0,4
syscall
move $a0,$t0
li $v0,1
syscall
la $a0,line
li $v0,4
syscall
j top
negative:
li $v0 , 10
syscall
CE-207 Computer Organization & Architecture SSUET/QR/114
OUTPUT:
CE-207 Computer Organization & Architecture SSUET/QR/114
2. Show the result of following expression: 2n , Where n is last digit of your Roll
No.
SOURCE CODE:
.data
msg:.asciiz "Enter Base : "
msg1:.asciiz "Enter Power : "
msg3: .asciiz "Output : "
.text
.globl main
main:
la $a0, msg
li $v0, 4
syscall
li $v0, 5
syscall
move $t0 , $v0
la $a0, msg1
li $v0, 4
syscall
li $v0, 5
syscall
move $t1, $v0
li $t2, 1
Loop:
beq $t1, 0, Exit
mul $t2, $t2, $t0
sub $t1, $t1, 1
b Loop
Exit:
la $a0, msg3
li $v0, 4
syscall
move $a0, $t2
li $v0, 1
syscall
li $v0, 10
syscall
CE-207 Computer Organization & Architecture SSUET/QR/114
OUTPUT: