Translate 4 C programs into MIPS assembly
MIPS programs translated from C code
part3.asm
# Declare main as a global function
.globl main
# All program code is placed after the
# .text assembler directive
.text
# The label ‘main’ represents the starting point
main:
lw $s0, Z #load z=2 into s0 register
lw $s1, i #load i=0(initialization of i) into s1 register
li $t1, 20
li $t2, 0
li $t3, 100
li $t4, 1
while:
#dont know how to do while(1)
bgt $s1, $t1, do #check if i>20, and switch to do loop
addi $s0, $s0, 1 #(z=z++)=(z=z+1)
addi $s1, $s1, 2 #i=i+2
j while
do:
addi $s0, $s0, 1 #(z=z++)=(z=z+1)
blt $s0, $t3, while2#move to while2 label after this instruction
j do #jump ot do while loop after going into the do loop
while2:
ble $s1, $t2, storewordandexit #jump to next label
sub $s0, $s0, $t4 #(z=z–)=(z=z-1)
sub $s1, $s1, $t4 #(i=i–)=(i=i-1)
j while2
storewordandexit:
la $t5, Z #load z
la $t6, i #load i
sw $s0, 0($t5) #store z in address
sw $s1, 0($t6) #store i in address
# Exit the program by means of a syscall.
# There are many syscalls – pick the desired one
# by placing its code in $v0. The code for exit is “10”
li $v0, 10 # Sets $v0 to “10” to select exit syscall
syscall # Exit
# All memory structures are placed after the
# .data assembler directive
# The .word assembler directive reserves space
# in memory for a single 4-byte word (or multiple 4-byte words)
# and assigns that memory location an initial value
# (or a comma separated list of initial values)
.data
Z: .word 2
i: .word 0
part4.asm
# Declare main as a global function
.globl main
# All program code is placed after the
# .text assembler directive
.text
# The label ‘main’ represents the starting point
main:
la $s1, A # Address of element at A
la $s2, B # Address of element at A
lw $s3, C #load C
lw $s4, i #load i
li $t1, 5 #temp val
li $t4, 0 #temp val
li $t5, 0 #temp val offset
li $s6, 1#for decrement
for:
bge $s4, $t1, while #go to while loop if i>=5
add $t6, $t5, $s2 #add offset value with the address for the array
lw $t2, 0($s6) #load value at ith position in memory for B[i]
add $t3, $t2, $s3 #add B[i]+c
add $t7, $s1, $t5 #add address of A with offset
sw $t3, 0($t7) #store values into A[i]
addi $s4, $s4, 1 # i++(increment)
addi $t5, $t5, 4 #incrementing offset by 4 (bytes)
j for #go back to for loop
sub $s4, $s4, $s6 #i=i-1
li $t6, 2 #for multiplication in while loop
li $t5, 16 # offset is 16
while:
blt $s4, $t4, exit
add $t7, $s1, $t5#add address of A with offset
lw $t3, 0($t7)#load value into ith element of A[i]
mul $t3, $t3, $t6 #while loop multiplication
sw $t3, 0($t7) #store value
addi $t5, $t5, -4 #incrementing offset by 4 (bytes)
sub $s4, $s4, $s6 #i=i-1
j while
exit:
# Exit the program by means of a syscall.
# There are many syscalls – pick the desired one
# by placing its code in $v0. The code for exit is “10”
li $v0, 10 # Sets $v0 to “10” to select exit syscall
syscall # Exit
# All memory structures are placed after the
# .data assembler directive
.data
# The .word assembler directive reserves space
# in memory for a single 4-byte word (or multiple 4-byte words)
# and assigns that memory location an initial value
# (or a comma separated list of initial values)
A: .space 20#Int(4 bytes)x5=20 bytes of space
B: .word 1,2,3,4,5
C: .word 12
i: .word 0
Assignment.s
.globl main
.text
li $s0, 0 # $s0 = i = 0
li $s1, 0 # $s1 = *result = NULL pointer
main:
# Obtain string from user, e.g. “Constantinople”
li $v0, 8 # 8 = syscall code to read string
la $a0, buffer # $a0 = address of buffer that will store input
li $a1, 256 # $a1 = 256 = max chars to put in buffer (last will be null)
syscall # Read in at most 255 characters and put them in buffer
# Search string for letter ‘e’.
# Result is pointer to first e (if it exists)
# or NULL pointer if it does not exist
while:
# Get &string[i] in $t0 and string[i] in $t1
la $t0, buffer # $t0 = base address of buffer
add $t0, $t0, $s0 # $t0 = base address + i = address of string[i]
lb $t1, 0($t0) # $t1 = string[i]
beqz $t1, endLoop # If string[i] is a null byte (0), break
# Check if string[i] is e
bne $t1, ‘e’, notE # If $t1 is not ‘e’, jump to label “notE”
# If we get here, we found ‘e’
move $s1, $t0 # result = &string[i]
j endLoop # break
notE: # Get here if we just checked a letter and it wasn’t ‘e’
addi $s0, $s0, 1 # i++
j while # Loop
endLoop:
beqz $s1, noMatchFound # If result == NULL, branch to “noMatchFound”
# If we get here, a match was found.
# First line of “match found” result:
li $v0, 4 # 4 = syscall code to print string
la $a0, firstMatch # $a0 = address of string “First match at address ”
syscall # Print “First match at address ”
li $v0, 1 # 1 = syscall code to print int (in this case, an address)
move $a0, $s1 # $a0 = result = int to print
syscall # Print result
li $v0, 11 # 11 = syscall code to print char (in this case, a new line)
li $a0, ‘\n’ # $a0 = ascii code for new line char
syscall # Print new line
# Second line of “match found” result:
li $v0, 4 # 4 = syscall code to print string
la $a0, matching # $a0 = address of string “The matching character is ”
syscall # Print “The matching character is ”
li $v0, 11 # 11 = syscall code to print char (in this case, the matching char)
move $a0, $t1 # $a0 = $t1 = matching char
syscall # Print matching char
li $v0, 11 # 11 = syscall code to print char (in this case, a new line)
li $a0, ‘\n’ # $a0 = ascii code for new line char
syscall # Print new line
# Exit
j exit
noMatchFound:
li $v0, 4 # 4 = syscall code to print string
la $a0, noMatch # $a0 = address of string “No match found\n”
syscall # Print “No match found\n”
exit:
li $v0, 10 # 10 = syscall code to exit
syscall # exit
.data
firstMatch: .asciiz “First match at address ”
matching: .asciiz “The matching character is ”
noMatch: .asciiz “No match found\n”
buffer: .byte 256 # 256 byte space in memory for input
Solution
globl main
.text
li $s0, 0 # $s0 = i = 0
li $s1, 0 # $s1 = *result = NULL pointer
main:
# Obtain string from user, e.g. “Constantinople”
li $v0, 8 # 8 = syscall code to read string
la $a0, buffer # $a0 = address of buffer that will store input
li $a1, 256 # $a1 = 256 = max chars to put in buffer (last will be null)
syscall # Read in at most 255 characters and put them in buffer
# Search string for letter ‘e’.
# Result is pointer to first e (if it exists)
# or NULL pointer if it does not exist
while:
# Get &string[i] in $t0 and string[i] in $t1
la $t0, buffer # $t0 = base address of buffer
add $t0, $t0, $s0 # $t0 = base address + i = address of string[i]
lb $t1, 0($t0) # $t1 = string[i]
beqz $t1, endLoop # If string[i] is a null byte (0), break
# Check if string[i] is e
bne $t1, ‘e’, notE # If $t1 is not ‘e’, jump to label “notE”
# If we get here, we found ‘e’
move $s1, $t0 # result = &string[i]
j endLoop # break
notE: # Get here if we just checked a letter and it wasn’t ‘e’
addi $s0, $s0, 1 # i++
j while # Loop
endLoop:
la $t0,result # save result in variable
sw $s1,($t0)
beqz $s1, noMatchFound # If result == NULL, branch to “noMatchFound”
# If we get here, a match was found.
# First line of “match found” result:
li $v0, 4 # 4 = syscall code to print string
la $a0, firstMatch # $a0 = address of string “First match at address ”
syscall # Print “First match at address ”
li $v0, 1 # 1 = syscall code to print int (in this case, an address)
move $a0, $s1 # $a0 = result = int to print
syscall # Print result
li $v0, 11 # 11 = syscall code to print char (in this case, a new line)
li $a0, ‘\n’ # $a0 = ascii code for new line char
syscall # Print new line
# Second line of “match found” result:
li $v0, 4 # 4 = syscall code to print string
la $a0, matching # $a0 = address of string “The matching character is ”
syscall # Print “The matching character is ”
li $v0, 11 # 11 = syscall code to print char (in this case, the matching char)
move $a0, $t1 # $a0 = $t1 = matching char
syscall # Print matching char
li $v0, 11 # 11 = syscall code to print char (in this case, a new line)
li $a0, ‘\n’ # $a0 = ascii code for new line char
syscall # Print new line
# Exit
j exit
noMatchFound:
li $v0, 4 # 4 = syscall code to print string
la $a0, noMatch # $a0 = address of string “No match found\n”
syscall # Print “No match found\n”
exit:
li $v0, 10 # 10 = syscall code to exit
syscall # exit
.data
firstMatch: .asciiz “First match at address ”
matching: .asciiz “The matching character is ”
noMatch: .asciiz “No match found\n”
result: .word 0
buffer: .byte 256 # 256 byte space in memory for input
part3.asm
# Declare main as a global function
.globl main
# All program code is placed after the
# .text assembler directive
.text
# The label ‘main’ represents the starting point
main:
lw $s0, Z #load z=2 into s0 register
lw $s1, i #load i=0(initialization of i) into s1 register
li $t1, 20
li $t2, 0
li $t3, 100
li $t4, 1
while:
#dont know how to do while(1)
bgt $s1, $t1, do #check if i>20, and switch to do loop
addi $s0, $s0, 1 #(z=z++)=(z=z+1)
addi $s1, $s1, 2 #i=i+2
j while
do:
addi $s0, $s0, 1 #(z=z++)=(z=z+1)
blt $s0, $t3, do #repeat do loop only if Z<100
while2:
ble $s1, $t2, storewordandexit #jump to next label
sub $s0, $s0, $t4 #(z=z–)=(z=z-1)
sub $s1, $s1, $t4 #(i=i–)=(i=i-1)
j while2
storewordandexit:
la $t5, Z #load z
la $t6, i #load i
sw $s0, 0($t5) #store z in address
sw $s1, 0($t6) #store i in address
# Exit the program by means of a syscall.
# There are many syscalls – pick the desired one
# by placing its code in $v0. The code for exit is “10”
li $v0, 10 # Sets $v0 to “10” to select exit syscall
syscall # Exit
# All memory structures are placed after the
# .data assembler directive
# The .word assembler directive reserves space
# in memory for a single 4-byte word (or multiple 4-byte words)
# and assigns that memory location an initial value
# (or a comma separated list of initial values)
.data
Z: .word 2
i: .word 0
part4.asm
# Declare main as a global function
.globl main
# All program code is placed after the
# .text assembler directive
.text
# The label ‘main’ represents the starting point
main:
la $s1, A # Address of element at A
la $s2, B # Address of element at A
lw $s3, C #load C
lw $s4, i #load i
li $t1, 5 #temp val
li $t4, 0 #temp val
li $t5, 0 #temp val offset
li $s6, 1#for decrement
for:
bge $s4, $t1, endfor #go to endfor if i>=5
add $t6, $t5, $s2 #add offset value with the address for the array
lw $t2, 0($t6) #load value at ith position in memory for B[i]
add $t3, $t2, $s3 #add B[i]+c
add $t7, $s1, $t5 #add address of A with offset
sw $t3, 0($t7) #store values into A[i]
addi $s4, $s4, 1 # i++(increment)
addi $t5, $t5, 4 #incrementing offset by 4 (bytes)
j for #go back to for loop
endfor:
sub $s4, $s4, $s6 #i=i-1
addi $t5, $t5, -4 #decrement offset by 4 (bytes)
li $t6, 2 #for multiplication in while loop
while:
blt $s4, $t4, exit
add $t7, $s1, $t5#add address of A with offset
lw $t3, 0($t7)#load value into ith element of A[i]
mul $t3, $t3, $t6 #while loop multiplication
sw $t3, 0($t7) #store value
addi $t5, $t5, -4 #incrementing offset by 4 (bytes)
sub $s4, $s4, $s6 #i=i-1
j while
exit:
# Exit the program by means of a syscall.
# There are many syscalls – pick the desired one
# by placing its code in $v0. The code for exit is “10”
li $v0, 10 # Sets $v0 to “10” to select exit syscall
syscall # Exit
# All memory structures are placed after the
# .data assembler directive
.data
# The .word assembler directive reserves space
# in memory for a single 4-byte word (or multiple 4-byte words)
# and assigns that memory location an initial value
# (or a comma separated list of initial values)
A: .space 20#Int(4 bytes)x5=20 bytes of space
B: .word 1,2,3,4,5
C: .word 12
i: .word 0