In: Computer Science
Write an assembly language program to input a string from the user. Your program should do these two things:
***** I AM POSTING THIS QUESTION FOR THE THIRD TIME NOW,I WANT IT DONE USING INCLUDE IRVINE32.INC*****
1. count and display the number of words in the user input string.
2. Flip the case of each character from upper to lower or lower to upper.
For example if the user types in: "Hello thEre. How aRe yOu?"
Your output should be:
The number of words in the input string is: 5
The output string is : hELLO THeRE. hOW ArE YoU?
Ans :
Write an assembly language program to input a string from the user. Your program should do these two things:
1. count and display the number of words in the user input string.
2. Flip the case of each character from upper to lower or lower to upper.
For example if the user types in: "Hello thEre. How aRe yOu?"
Your output should be:
The number of words in the input string is: 5
The output string is : hELLO THeRE. hOW ArE YoU?
Below is the C Code Program for the above question:
=======================
$ cat string_conversion.c
/*
* C Program to Count Number of Words in a given Text Or
Sentence
*/
#include <stdio.h>
#include <string.h>
int main()
{
char s[200];
int count = 0, i;
printf("Enter the String\n");
scanf("%[^\n]s", s);
for (i = 0;s[i] != '\0';i++)
{
if (s[i] == ' ')
count++;
}
printf("Number of Words in Given String are: %d\n", count + 1);
printf("Old String is: %s\n", s);
for(int i=0; s[i]!='\0'; i++)
{
if (s[i]>='a' && s[i]<='z')
{
//Convert lowercase to uppercase
s[i] = s[i] - 32;
}
else if(s[i]>='A' && s[i]<='Z')
{
//Convert uppercase to lowercase
s[i] = s[i] + 32;
}
}
//print the converted string format
printf("New String is %s\n", s);
return 0;
}
==============================================
Write the above C Code in g++ compiler compatible operating systems.
Compile the C Code using g++ command
$ g++ -O string_conversion.c
Above linux command will generate a.out output file. Execute the ./a.out to know the results:
$ ./a.out
Enter the String
Hello thEre. How aRe yOu?
Number of Words in Given String are: 5
Old String is: Hello thEre. How aRe yOu?
New String is hELLO THeRE. hOW ArE YoU?
Convert C Code to Assembly Code using g++ command
$ g++ -S string_conversion.c
this will generate string_conversion.s assembly code and below are the contents
$ cat string_conversion.s
.LC0:
.string "Enter the String"
.LC1:
.string "%[^\n]s"
.LC2:
.string "Number of Words in Given String are: %d\n"
.LC3:
.string "Old String is: %s\n"
.LC4:
.string "New String is %s\n"
main:
push rbp
mov rbp, rsp
sub rsp, 224
mov DWORD PTR [rbp-4], 0
mov edi, OFFSET FLAT:.LC0
call puts
lea rax, [rbp-224]
mov rsi, rax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call scanf
mov DWORD PTR [rbp-8], 0
.L4:
mov eax, DWORD PTR [rbp-8]
cdqe
movzx eax, BYTE PTR [rbp-224+rax]
test al, al
je .L2
mov eax, DWORD PTR [rbp-8]
cdqe
movzx eax, BYTE PTR [rbp-224+rax]
cmp al, 32
jne .L3
add DWORD PTR [rbp-4], 1
.L3:
add DWORD PTR [rbp-8], 1
jmp .L4
.L2:
mov eax, DWORD PTR [rbp-4]
add eax, 1
mov esi, eax
mov edi, OFFSET FLAT:.LC2
mov eax, 0
call printf
lea rax, [rbp-224]
mov rsi, rax
mov edi, OFFSET FLAT:.LC3
mov eax, 0
call printf
mov DWORD PTR [rbp-12], 0
.L8:
mov eax, DWORD PTR [rbp-12]
cdqe
movzx eax, BYTE PTR [rbp-224+rax]
test al, al
je .L5
mov eax, DWORD PTR [rbp-12]
cdqe
movzx eax, BYTE PTR [rbp-224+rax]
cmp al, 96
jle .L6
mov eax, DWORD PTR [rbp-12]
cdqe
movzx eax, BYTE PTR [rbp-224+rax]
cmp al, 122
jg .L6
mov eax, DWORD PTR [rbp-12]
cdqe
movzx eax, BYTE PTR [rbp-224+rax]
sub eax, 32
mov edx, eax
mov eax, DWORD PTR [rbp-12]
cdqe
mov BYTE PTR [rbp-224+rax], dl
jmp .L7
.L6:
mov eax, DWORD PTR [rbp-12]
cdqe
movzx eax, BYTE PTR [rbp-224+rax]
cmp al, 64
jle .L7
mov eax, DWORD PTR [rbp-12]
cdqe
movzx eax, BYTE PTR [rbp-224+rax]
cmp al, 90
jg .L7
mov eax, DWORD PTR [rbp-12]
cdqe
movzx eax, BYTE PTR [rbp-224+rax]
add eax, 32
mov edx, eax
mov eax, DWORD PTR [rbp-12]
cdqe
mov BYTE PTR [rbp-224+rax], dl
.L7:
add DWORD PTR [rbp-12], 1
jmp .L8
.L5:
lea rax, [rbp-224]
mov rsi, rax
mov edi, OFFSET FLAT:.LC4
mov eax, 0
call printf
mov eax, 0
leave
ret
Write the above Assembly Code Language in 8085/8086 compiler compatible operating systems.
Compile and Execute the Assembly Code Language. Validate and Verify the results (with different input-strings)