The goal of this lab is to get familiar with how the program described in high-l
ID: 3754655 • Letter: T
Question
The goal of this lab is to get familiar with how the program described in high-level language (particularly C) is translated into MIPS assembly language. We will eventually build our own simulator in the next lab assignment. For this lab assignment, we don't have a simulator to run MIPS assembly, so we will use an existing one, called MARS. A basic MARS tutorial will be provided separately Specifically, translate the following C procedure into conventional use for registers such as temporary vs. saved registers, maintaining procedure call stacks, proper parameter passing and returns, etc. Note that this procedure calls another (strchr) which is assumed to also comply with those conventions. MIPS Assembly. You are required to follow the char firstmatch (char *s, char *s2 find the first character in string s1 that is also in s2* char *temp temp sl; do if (strchr( s2, temp)!-0)if this character is there/ return where we found it* else look again return temp temp++; while (temp return 0: 0) *found none of these chars/ char * strchr (register const char *s, int c) do if (sc return (char*)s while (s++) return (0); These are additional instructions to complete this lab assignment. The program should consist of two segments: .data and text segments. Define two strings, str and str2, in the data segment. Use lb (load byte) as you manipulate strings not integers. . The code segment, labeled as "text", should begin with "main" because the MARS simulator begins the execution from there In the main code, you should start with load addresses of the defined strl and str2 You need to write two procedures, firstmatch and strchr, in the code segment, each is labeled as the procedure name After loading string addresses, you pass both strings to firstmatch as in the above C code Within the firstmatch procedure, you need to call the strchr procedure.Explanation / Answer
/* ------------- C input code -------------------- */
#include <stdio.h>
/*
* Note: Here I just added the main function to load s1, s2 addresses.
* And, modified the code to get desired output.
*/
char *strchr(register const char *s, int c)
{
do{
if (*s == c)
{
return (char*)s;
}
}while(*s++);
return(0);
}
char firstmatch(char *s1, char *s2) /* find the first character in string s1 that is also in s2 */
{
char *temp;
temp = s1;
do {
if (strchr(s2, *temp)!=0) /* if this character is there */
return *temp; /* input code modified code*/ /* return where we found it */
temp++; /* else look again */
}while(*temp != 0);
return 0; /* found none of these chars */
}
int main()
{
char s1[] = "TheCheggTeam";
char s2[] = "Chegg";
char firstchar = firstmatch(s1, s2);
if (firstchar == 0 /* NULL */)
return -1;
printf("first matched char : %c", firstchar);
return 0;
}
/* ----------------- Corresponding MIPS Assembly Code ------------ */
.file 1 ""
.section .mdebug.abi32
.previous
.nan legacy
.module fp=32
.module nooddspreg
.abicalls
.text
.align 2
.globl strchr
.set nomips16
.set nomicromips
.ent strchr
.type strchr, @function
strchr:
.frame $fp,8,$31 # vars= 0, regs= 1/0, args= 0, gp= 0
.mask 0x40000000,-4
.fmask 0x00000000,0
.set noreorder
.set nomacro
addiu $sp,$sp,-8
sw $fp,4($sp)
move $fp,$sp
move $2,$4
sw $5,12($fp)
$L4:
movz $31,$31,$0
lb $3,0($2)
nop
move $4,$3
lw $3,12($fp)
nop
bne $4,$3,$L2
nop
b $L3
nop
$L2:
move $3,$2
addiu $2,$3,1
lb $3,0($3)
nop
bne $3,$0,$L4
nop
move $2,$0
$L3:
move $sp,$fp
lw $fp,4($sp)
addiu $sp,$sp,8
j $31
nop
.set macro
.set reorder
.end strchr
.size strchr, .-strchr
.align 2
.globl firstmatch
.set nomips16
.set nomicromips
.ent firstmatch
.type firstmatch, @function
firstmatch:
.frame $fp,40,$31 # vars= 8, regs= 2/0, args= 16, gp= 8
.mask 0xc0000000,-4
.fmask 0x00000000,0
.set noreorder
.cpload $25
.set nomacro
addiu $sp,$sp,-40
sw $31,36($sp)
sw $fp,32($sp)
move $fp,$sp
.cprestore 16
movz $31,$31,$0
sw $4,40($fp)
sw $5,44($fp)
lw $2,40($fp)
nop
sw $2,24($fp)
$L8:
lw $2,24($fp)
nop
lb $2,0($2)
nop
move $5,$2
lw $4,44($fp)
lw $2,%got(strchr)($28)
nop
move $25,$2
.reloc 1f,R_MIPS_JALR,strchr
1: jalr $25
nop
lw $28,16($fp)
beq $2,$0,$L6
nop
lw $2,24($fp)
nop
lb $2,0($2)
b $L7
nop
$L6:
lw $2,24($fp)
nop
addiu $2,$2,1
sw $2,24($fp)
lw $2,24($fp)
nop
lb $2,0($2)
nop
bne $2,$0,$L8
nop
move $2,$0
$L7:
move $sp,$fp
lw $31,36($sp)
lw $fp,32($sp)
addiu $sp,$sp,40
j $31
nop
.set macro
.set reorder
.end firstmatch
.size firstmatch, .-firstmatch
.rdata
.align 2
$LC1:
.ascii "first matched char : %c"
.align 2
$LC0:
.ascii "TheCheggTeam"
.text
.align 2
.globl main
.set nomips16
.set nomicromips
.ent main
.type main, @function
main:
.frame $fp,64,$31 # vars= 32, regs= 2/0, args= 16, gp= 8
.mask 0xc0000000,-4
.fmask 0x00000000,0
.set noreorder
.cpload $25
.set nomacro
addiu $sp,$sp,-64
sw $31,60($sp)
sw $fp,56($sp)
move $fp,$sp
.cprestore 16
movz $31,$31,$0
lw $2,%got($LC0)($28)
nop
lw $5,%lo($LC0)($2)
addiu $3,$2,%lo($LC0)
lw $4,4($3)
addiu $3,$2,%lo($LC0)
lw $3,8($3)
sw $5,28($fp)
sw $4,32($fp)
sw $3,36($fp)
addiu $2,$2,%lo($LC0)
lbu $2,12($2)
nop
sb $2,40($fp)
li $2,1130889216 # 0x43680000
ori $2,$2,0x6567
sw $2,44($fp)
li $2,26368 # 0x6700
sh $2,48($fp)
addiu $3,$fp,44
addiu $2,$fp,28
move $5,$3
move $4,$2
lw $2,%got(firstmatch)($28)
nop
move $25,$2
.reloc 1f,R_MIPS_JALR,firstmatch
1: jalr $25
nop
lw $28,16($fp)
sb $2,24($fp)
lb $2,24($fp)
nop
bne $2,$0,$L10
nop
li $2,-1 # 0xffffffffffffffff
b $L12
nop
$L10:
lb $2,24($fp)
nop
move $5,$2
lw $2,%got($LC1)($28)
nop
addiu $4,$2,%lo($LC1)
lw $2,%call16(printf)($28)
nop
move $25,$2
.reloc 1f,R_MIPS_JALR,printf
1: jalr $25
nop
lw $28,16($fp)
move $2,$0
$L12:
move $sp,$fp
lw $31,60($sp)
lw $fp,56($sp)
addiu $sp,$sp,64
j $31
nop
.set macro
.set reorder
.end main
.size main, .-main
/* ---------------- MIPS Assembly Code --------------- */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.