77.6k views
3 votes
19. Write a C program that converts an uppercase character to a lowercase character. Declare function char toLower(char ch); to do the conversion. If ch is not an uppercase character, the function should return ch unchanged. If it is an uppercase character, add the difference of 'a' and 'A' to ch as the return value. (a) Write your program with a global variable for the actual parameter. Translate your C program to Pep/9 assembly language. (b) Write your program with a local variable for the actual parameter. Translate your C program to Pep/9 assembly language

User Sigute
by
5.7k points

1 Answer

1 vote

Answer:

The program is written in C language and converts an uppercase character to lower case and in the first part of the question it uses global variable for actual parameter then the second part it uses local variable. In both parts after doing the conversion of character, we translate the C program to Pep/9 assembly language.

(a)

#include <stdio.h>

char ch;

void toLower(){

if ( ch <= 'Z' && ch >= 'A' )

ch += 'a' - 'A';

}

int main(){

printf("\\Enter the character in uppercase: ");

scanf("%c", &ch);

toLower();

printf("\\Output after conversion: %c\\\\", ch);

return 0;

}

Assembly conversion:

.file "toUp.c"

.comm ch,1,1

.text

.globl toLower

.type toLower, @function

toLower:

.LFB0:

.cfi_startproc

pushl %ebp

.cfi_def_cfa_offset 8

.cfi_offset 5, -8

movl %esp, %ebp

.cfi_def_cfa_register 5

movzbl ch, %eax

cmpb $90, %al

jg .L1

movzbl ch, %eax

cmpb $64, %al

jle .L1

movzbl ch, %eax

addl $32, %eax

movb %al, ch

.L1:

popl %ebp

.cfi_def_cfa 4, 4

.cfi_restore 5

ret

.cfi_endproc

.LFE0:

.size toLower, .-toLower

.section .rodata

.align 4

.LC0:

.string "\\Enter the character in uppercase: "

.LC1:

.string "%c"

.align 4

.LC2:

.string "\\Output after conversion: %c\\\\"

.text

.globl main

.type main, @function

main:

.LFB1:

.cfi_startproc

pushl %ebp

.cfi_def_cfa_offset 8

.cfi_offset 5, -8

movl %esp, %ebp

.cfi_def_cfa_register 5

andl $-16, %esp

subl $16, %esp

movl $.LC0, %eax

movl %eax, (%esp)

call printf

movl $.LC1, %eax

movl $ch, 4(%esp)

movl %eax, (%esp)

call __isoc99_scanf

call toLower

movzbl ch, %eax

movsbl %al, %edx

movl $.LC2, %eax

movl %edx, 4(%esp)

movl %eax, (%esp)

call printf

movl $0, %eax

leave

.cfi_restore 5

.cfi_def_cfa 4, 4

ret

.cfi_endproc

.LFE1:

.size main, .-main

.ident

(b)

#include <stdio.h>

char toLower(char ch){

if ( ch <= 'Z' && ch >= 'A' )

ch += 'a' - 'A';

return ch;

}

int main(){

char input, converted;

printf("\\Enter the character in uppercase: ");

scanf("%c", &input);

converted = toLower(input);

printf("\\Output after conversion: %c\\\\", converted);

return 0;

}

Assembly Code:

.file "toUp.c"

.text

.globl toLower

.type toLower, @function

toLower:

.LFB0:

.cfi_startproc

pushl %ebp

.cfi_def_cfa_offset 8

.cfi_offset 5, -8

movl %esp, %ebp

.cfi_def_cfa_register 5

subl $4, %esp

movl 8(%ebp), %eax

movb %al, -4(%ebp)

cmpb $90, -4(%ebp)

jg .L2

cmpb $64, -4(%ebp)

jle .L2

movzbl -4(%ebp), %eax

addl $32, %eax

movb %al, -4(%ebp)

.L2:

movzbl -4(%ebp), %eax

leave

.cfi_restore 5

.cfi_def_cfa 4, 4

ret

.cfi_endproc

.LFE0:

.size toLower, .-toLower

.section .rodata

.align 4

.LC0:

.string "\\Enter the character in uppercase: "

.LC1:

.string "%c"

.align 4

.LC2:

.string "\\Output after conversion: %c\\\\"

.text

.globl main

.type main, @function

main:

.LFB1:

.cfi_startproc

pushl %ebp

.cfi_def_cfa_offset 8

.cfi_offset 5, -8

movl %esp, %ebp

.cfi_def_cfa_register 5

andl $-16, %esp

subl $32, %esp

movl $.LC0, %eax

movl %eax, (%esp)

call printf

movl $.LC1, %eax

leal 30(%esp), %edx

movl %edx, 4(%esp)

movl %eax, (%esp)

call __isoc99_scanf

movzbl 30(%esp), %eax

movsbl %al, %eax

movl %eax, (%esp)

call toLower

movb %al, 31(%esp)

movsbl 31(%esp), %edx

movl $.LC2, %eax

movl %edx, 4(%esp)

movl %eax, (%esp)

call printf

movl $0, %eax

leave

.cfi_restore 5

.cfi_def_cfa 4, 4

ret

.cfi_endproc

.LFE1:

.size main, .-main

.ident

User Riya Kapuria
by
5.7k points