Convert the following C code into MIPS using MARS simulator: Part a #include int
ID: 3796210 • Letter: C
Question
Convert the following C code into MIPS using MARS simulator:
Part a
#include
int main()
{
// Declare string
char name[100];
// Read string
printf("Enter your name: ");
scanf("%s", name);
// Print string
printf("Hello, %s ", name);
return 0;
}
Part b
#include
int main()
{
// Read string
char s[20];
printf("Enter a string: ");
scanf("%s", s);
// Check first character
char c = s[0];
if (c >= 'a' && c <= 'z')
{
printf("The first character is a lower-case letter ");
}
else
{
printf("The first character is not a lower-case letter ");
}
// Done
return 0;
}
Explanation / Answer
Part 1:
.LC0:
.string "Enter your name: "
.LC1:
.string "%s"
.LC2:
.string "Hello, %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 rsi, rax
mov edi, OFFSET FLAT:.LC2
mov eax, 0
call printf
mov eax, 0
leave
ret
Part 2:
.LC0:
.string "Enter a string: "
.LC1:
.string "%s"
.LC2:
.string "The first character is a lower-case letter"
.LC3:
.string "The first character is not a lower-case letter"
main:
push rbp
mov rbp, rsp
sub rsp, 32
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call printf
lea rax, [rbp-32]
mov rsi, rax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call scanf
movzx eax, BYTE PTR [rbp-32]
mov BYTE PTR [rbp-1], al
cmp BYTE PTR [rbp-1], 96
jle .L2
cmp BYTE PTR [rbp-1], 122
jg .L2
mov edi, OFFSET FLAT:.LC2
call puts
jmp .L3
.L2:
mov edi, OFFSET FLAT:.LC3
call puts
.L3:
mov eax, 0
leave
ret