Question

In: Computer Science

Write an assembly language program to input a string from the user. Your program should do...

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?

Solutions

Expert Solution

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)


Related Solutions

Write an X86-series assembly language program that checks whether input string is palindrome or not. A...
Write an X86-series assembly language program that checks whether input string is palindrome or not. A palindrome is a word, number, phrase or any other sequence which reads the same backward as forward e.g. madam, racecar. Sample Execution: Please enter a String: redivider The string is a palindrome Another Sample Execution: Please enter a String: abracadabra The string is not a palindrome
Write a program that takes a string input from the user and then outputs the first...
Write a program that takes a string input from the user and then outputs the first character, then the first two, then the first three, etc until it prints the entire word. After going up to the full word, go back down to a single letter. LastNameUpDown. Input: Kean Output: K Ke Kea Kean Kea Ke K
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
arms64 assembly language Write a function to read a Sudoku board from an input string. The...
arms64 assembly language Write a function to read a Sudoku board from an input string. The input string must be exactly 81 characters long (plus the terminating null that marks the end of the string) and contains digits and dots (the `.` character represents an unmarked position). The input contains all 9 rows packed together. For example, a Sudoku board that looks like this: ``` ..7 ... ... 6.4 ... ..3 ... .54 ..2 ... .4. ... 9.. ... ..5...
Write a program that prompts the user to input a string. The program then uses the...
Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning of your program and...
Write a program that encrypts and decrypts the user input. Note – Your input should be...
Write a program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x) given by...
Using MARS write a MIPS assembly language program to prompt the user to input two 32-bit...
Using MARS write a MIPS assembly language program to prompt the user to input two 32-bit integers X and Y (X and Y can be prompted separately or at the same time), get them from the user then store them in memory locations labeled X and Y respectively. The program then loads X and Y from the main memory to registers, calculates the sum of them (i.e. X + Y) and store the sum into a memory location labeled S....
Write a MIPS assembly program that prompts the user first for a string, then for a...
Write a MIPS assembly program that prompts the user first for a string, then for a character. The program should then search the string for the character and print out the number of times the character appears in the string. You can use as many restrictions as you want, just be sure to list them. You must allocate space in the .data segment of your program for the user string.
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
Write a simple MIPS program that asks the user to input a string and then a...
Write a simple MIPS program that asks the user to input a string and then a character. The program should then count how many times that character appears in the string and print out that value. Please use comments.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT