Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the StringLengthFunction that takes a strin\'s address as input in reg

ID: 3721615 • Letter: I

Question

Implement the StringLengthFunction that takes a strin's address as input in register $a1 and Returns the string's length in $v0. No need to print the length to the console. Simply return the length.

                               .data

theString:                .acsiiz                                   "This is the string to find the length of."

                              .code

                              .globl                                     main

main:                       la                                         $a1,the String

                               jal                                         $stringLength

                               syscall                                 $exit

Explanation / Answer


StringLengthFunction(char*):
push rbp
mov rbp, rsp
mov QWORD PTR [rbp-24], rdi
mov DWORD PTR [rbp-4], 0
.L3:
mov eax, DWORD PTR [rbp-4]
movsx rdx, eax
mov rax, QWORD PTR [rbp-24]
add rax, rdx
movzx eax, BYTE PTR [rax]
test al, al
je .L2
mov eax, DWORD PTR [rbp-4]
add eax, 1
mov DWORD PTR [rbp-4], eax
jmp .L3
.L2:
mov eax, DWORD PTR [rbp-4]
pop rbp
ret
.LC0:
.string "Enter a string: "
.LC1:
.string "%s"
main:
push rbp
mov rbp, rsp
sub rsp, 112
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call printf
lea rax, [rbp-112]
mov rsi, rax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call scanf
lea rax, [rbp-112]
mov rdi, rax
call StringLengthFunction(char*)
mov DWORD PTR [rbp-4], eax
mov eax, 0
leave
ret

StringLengthFunction(char*):

push rbp

mov rbp, rsp

mov QWORD PTR [rbp-24], rdi

mov DWORD PTR [rbp-4], 0

.L3:

mov eax, DWORD PTR [rbp-4]

movsx rdx, eax

mov rax, QWORD PTR [rbp-24]

add rax, rdx

movzx eax, BYTE PTR [rax]

test al, al

je .L2

mov eax, DWORD PTR [rbp-4]

add eax, 1

mov DWORD PTR [rbp-4], eax

jmp .L3

.L2:

mov eax, DWORD PTR [rbp-4]

pop rbp

ret

.LC0:

.string "Enter a string: "

.LC1:

.string "%s"

main:

push rbp

mov rbp, rsp

sub rsp, 112

mov edi, OFFSET FLAT:.LC0

mov eax, 0

call printf

lea rax, [rbp-112]

mov rsi, rax

mov edi, OFFSET FLAT:.LC1

mov eax, 0

call scanf

lea rax, [rbp-112]

mov rdi, rax

call StringLengthFunction(char*)

mov DWORD PTR [rbp-4], eax

mov eax, 0

leave

ret
StringLengthFunction(char*):
push rbp
mov rbp, rsp
mov QWORD PTR [rbp-24], rdi
mov DWORD PTR [rbp-4], 0
.L3:
mov eax, DWORD PTR [rbp-4]
movsx rdx, eax
mov rax, QWORD PTR [rbp-24]
add rax, rdx
movzx eax, BYTE PTR [rax]
test al, al
je .L2
mov eax, DWORD PTR [rbp-4]
add eax, 1
mov DWORD PTR [rbp-4], eax
jmp .L3
.L2:
mov eax, DWORD PTR [rbp-4]
pop rbp
ret
.LC0:
.string "Enter a string: "
.LC1:
.string "%s"
main:
push rbp
mov rbp, rsp
sub rsp, 112
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call printf
lea rax, [rbp-112]
mov rsi, rax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call scanf
lea rax, [rbp-112]
mov rdi, rax
call StringLengthFunction(char*)
mov DWORD PTR [rbp-4], eax
mov eax, 0
leave
ret