Question: MIPS Assembly language. Implement memset, strlen, strchr, strcat, and strcmp. Use $a0 through $a3 as necessary for the standard C library's arguments to each function.

MIPS Assembly language.
Implement memset, strlen, strchr, strcat, and strcmp.
Use $a0 through $a3 as necessary for the standard C library's arguments to each function.
Return in $v0.
It is fine if a lot of your functions look a lot like each other. In fact, that's a good sign.
Don't worry about overruns or exception cases.
Implement strstr. Dont try this until youve finished the others (strstr is a lot more annoying to implement)
Testing program:
.data # The data segment.
welc1: .asciiz "Welcome to the test. "
welc2: .asciiz "First, strcpy. This should print 'Hello, world.' twice. "
welc3: .asciiz "Now, strlen. Should be 13: "
welc4: .asciiz "Now, memset. Should print 13 dots: "
welc5: .asciiz "Now, strchr. Should be a number, that +3, then zero. "
welc6: .asciiz "Now, strcat. Should be dots then 'Hello, world.'. "
welc7: .asciiz "Finally, strcmp. Should be 0, then 1, then -1. "
hello: .asciiz "Hello, world."
alice: .asciiz "alice"
bob: .asciiz "bob"
crlf: .asciiz " "
dot: .byte 46
stch: .byte 'l'
buf: .space 128
.text # The code segment.
main:
# --- WELCOME ---
li $v0, 4 # System call to print.
la $a0, welc1 # Load up our message.
syscall # Fire the system call.
# --- strcpy ---
li $v0, 4 # System call to print.
la $a0, welc2 # Load up our message.
syscall # Fire the system call.
li $v0, 4 # System call to print.
la $a0, hello # Load up our message.
syscall # Fire the system call.
li $v0, 4 # System call to print.
la $a0, crlf # Load up our message.
syscall # Fire the system call.
la $a0, buf
la $a1, hello
jal strcpy # strcpy(buf, hello)
move $a0, $v0 # Load up the returned message.
li $v0, 4 # System call to print.
syscall # Fire the system call.
li $v0, 4 # System call to print.
la $a0, crlf # Load up our message.
syscall # Fire the system call.
# --- strlen ---
li $v0, 4 # System call to print.
la $a0, welc3 # Load up our message.
syscall # Fire the system call.
la $a0, buf
jal strlen
move $s0, $v0
move $a0, $v0
li $v0, 1
syscall
li $v0, 4 # System call to print.
la $a0, crlf # Load up our message.
syscall # Fire the system call.
# --- memset ---
li $v0, 4 # System call to print.
la $a0, welc4 # Load up our message.
syscall # Fire the system call.
la $a0, buf
lb $a1, dot
move $a2, $s0
jal memset
move $a0, $v0 # Load up the returned message.
li $v0, 4 # System call to print.
syscall # Fire the system call.
li $v0, 4 # System call to print.
la $a0, crlf # Load up our message.
syscall # Fire the system call.
# --- strchr ---
li $v0, 4 # System call to print.
la $a0, welc5 # Load up our message.
syscall # Fire the system call.
la $a0, hello
li $v0, 1
syscall
li $v0, 4 # System call to print.
la $a0, crlf # Load up our message.
syscall # Fire the system call.
la $a0, hello
lb $a1, stch
jal strchr
move $a0, $v0
li $v0, 1
syscall
li $v0, 4 # System call to print.
la $a0, crlf # Load up our message.
syscall # Fire the system call.
la $a0, buf
lb $a1, stch
jal strchr
move $a0, $v0
li $v0, 1
syscall
li $v0, 4 # System call to print.
la $a0, crlf # Load up our message.
syscall # Fire the system call.
# --- strcat ---
li $v0, 4 # System call to print.
la $a0, welc6 # Load up our message.
syscall # Fire the system call.
la $a0, buf
la $a1, hello
jal strcat
move $a0, $v0 # Load up the returned message.
li $v0, 4 # System call to print.
syscall # Fire the system call.
li $v0, 4 # System call to print.
la $a0, crlf # Load up our message.
syscall # Fire the system call.
# --- strcmp ---
li $v0, 4 # System call to print.
la $a0, welc7 # Load up our message.
syscall # Fire the system call.
la $a0, alice
la $a1, alice
jal strcmp
move $a0, $v0
li $v0, 1
syscall
li $v0, 4 # System call to print.
la $a0, crlf # Load up our message.
syscall # Fire the system call.
la $a0, bob
la $a1, alice
jal strcmp
move $a0, $v0
li $v0, 1
syscall
li $v0, 4 # System call to print.
la $a0, crlf # Load up our message.
syscall # Fire the system call.
la $a0, alice
la $a1, bob
jal strcmp
move $a0, $v0
li $v0, 1
syscall
li $v0, 4 # System call to print.
la $a0, crlf # Load up our message.
syscall # Fire the system call.
# --- finished ---
li $v0, 10 # System call to exit.
syscall # Fire the system call.
strcpy:
# strcpy(dest, src) copies src into dest and returns dest.
# dest = $a0, src = $a1, return $a0 in $v0.
addi $sp, $sp, -4
sw $ra, 0($sp)
move $v0, $a0 # Do this now so we don't have to save $a0
strcpy_next:
lb $t0, 0($a1) # Load a byte
sb $t0, 0($a0) # Save a byte
addi $a0, $a0, 1 # Increment dest
addi $a1, $a1, 1 # Increment src
bne $t0, $zero, strcpy_next # Done?
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
# Copy new code here.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!