In: Computer Science
#include <stdio.h>
#include <stdint.h>
char sz_1[] = "Upper/LOWER.";
char sz_2[] = "mIXeD CaSe..";
/* copies psz_2 to psz_1, downcases all letters */
void dostr (char* psz_1,char* psz_2) {
uint8_t u8_c;
while (*psz_2 != 0) {
u8_c = *psz_2;
if (u8_c > 0x2F) {
/* make sure it is not a special char */
*psz_1 = u8_c | 0x20; /* sets this bit */
} else {
/* leave special chars alone */
*psz_1 = u8_c;
}
psz_1++;
psz_2++;
}
}
int main(void) {
// Bug: MPLAB X v1.80 printf bug means strings vis %s don't print
correctly.
// So, break printf into two statements.
printf("Before...\n");
printf("sz_1: '"); printf(sz_1); printf("'\n");
printf("sz_2: '"); printf(sz_2); printf("'\n");
dostr(sz_1,sz_2);
printf("After...\n");
printf("sz_1: '"); printf(sz_1); printf("'\n");
printf("sz_2: '"); printf(sz_2); printf("'\n");
return 0;
}
convert it in assembly
Answer.
Step 1
ASSEMBLY LANGUAGE:
Step 2
CONVERTED ASSEMBLY CODE:
sz_1:
.string "Upper/LOWER."
sz_2:
.string "mIXeD CaSe.."
dostr: // these block of statements defines the method signature
push rbp
mov rbp, rsp
mov QWORD PTR [rbp-24], rdi
mov QWORD PTR [rbp-32], rsi
jmp .L2 // start the while loop
.L5:
mov rax, QWORD PTR [rbp-32] // these statements are for assigning the pointer value
movzx eax, BYTE PTR [rax]
mov BYTE PTR [rbp-1], al
cmp BYTE PTR [rbp-1], 47 // statements for the if condition
jbe .L3
movzx eax, BYTE PTR [rbp-1] // statements for pointer assignments
or eax, 32
mov edx, eax
mov rax, QWORD PTR [rbp-24]
mov BYTE PTR [rax], dl
jmp .L4
.L3:
movzx edx, BYTE PTR [rbp-1] // again statements for pointer assignments
mov rax, QWORD PTR [rbp-24]
mov BYTE PTR [rax], dl
.L4:
add QWORD PTR [rbp-24], 1 // increment in pointer variable
add QWORD PTR [rbp-32], 1 // again increment in the pointer variable
.L2:
mov rax, QWORD PTR [rbp-32]
movzx eax, BYTE PTR [rax]
test al, al
jne .L5
nop
nop
pop rbp
ret
.LC0:
.string "Before..."
.LC1:
.string "sz_1: '"
.LC2:
.string "'"
.LC3:
.string "sz_2: '"
.LC4:
.string "After..."
main: // start the main function definition
push rbp
mov rbp, rsp
mov edi, OFFSET FLAT:.LC0 // printf statement conversion
call puts
mov edi, OFFSET FLAT:.LC1 // display the value using printf command
mov eax, 0
call printf
mov edi, OFFSET FLAT:sz_1
mov eax, 0
call printf
mov edi, OFFSET FLAT:.LC2
call puts
mov edi, OFFSET FLAT:.LC3 // command to display the value using printf
mov eax, 0
call printf
mov edi, OFFSET FLAT:sz_2
mov eax, 0
call printf
mov edi, OFFSET FLAT:.LC2
call puts
mov esi, OFFSET FLAT:sz_2 // calls the function with the required parameters
mov edi, OFFSET FLAT:sz_1
call dostr
mov edi, OFFSET FLAT:.LC4 // print the space using printf command
call puts
mov edi, OFFSET FLAT:.LC1 // print the command using printf command
mov eax, 0
call printf
mov edi, OFFSET FLAT:sz_1
mov eax, 0
call printf
mov edi, OFFSET FLAT:.LC2
call puts
mov edi, OFFSET FLAT:.LC3 // display the values using the printf command
mov eax, 0
call printf
mov edi, OFFSET FLAT:sz_2
mov eax, 0
call printf
mov edi, OFFSET FLAT:.LC2
call puts
mov eax, 0 // return 0 command
pop rbp // closing curly braces
ret
Kindly upvote please,
Thank you.