Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to create a code in assembly that will READ AN IMPUT FROM THE USER and re

ID: 3801281 • Letter: I

Question

I need to create a code in assembly that will READ AN IMPUT FROM THE USER and reverse the string using an array as you can see being called in my code below. The purpose of the array is so the program knows which character to swap with which character in the loop called afterward. First with Last, Second to Second to the last, etc.. For example if the user imputs "Hello" output is "olleH"; "abcdef" outputs "fedcba"... etc. Please do not give me a a different version of your own type of code. I would like my variables and calls to remain the same and simply have my code fixed and additional comments if applicable to get a better way of learning. Thank you.

Here is my code so far:
-----------------------------------------------------------------------------------------------------------------------

INCLUDE Irvine32.inc

.data
MAX = 80
myString BYTE MAX+1 DUP (?)
myLength DWORD ?
numLoops DWORD ?

.code
Main PROC
call Clrscr

mov edx,OFFSET myString  ;buffer size -1
mov ecx,MAX
call ReadString
call StrLength  ;stores string length in eax

mov myLength,eax

xor edx,edx
mov ebx,2
div ebx

mov numLoops,eax
;ebx is left, edx is right, ecx current
mov ebx,OFFSET myString
mov edx,OFFSET myString
add edx,myLength
dec edx

mov ecx,numLoops

L1:
mov eax,0
mov al,[numLoops]
mov [eax],al
dec numLoops
inc eax
call WriteString
call Crlf

;inc ebx
;dec edx
loop L1
mov numLoops,OFFSET myString
mov ebx, 1
mov ecx, myLength-ebx
call DumpRegs
exit
main ENDP

END main
------------------------------------------------------------------------------------------------

Explanation / Answer

#include<stdio.h>

void reverse(char *begin, char *end);


void reverseWords(char *s)
{
char *word_begin = s;
char *temp = s; /* temp is for word boundry */
while( *temp )
{
temp++;
if (*temp == '')
{
reverse(word_begin, temp-1);
}
else if(*temp == ' ')
{
reverse(word_begin, temp-1);
word_begin = temp+1;
}
}

reverse(s, temp-1);
}

void reverse(char *begin, char *end)
{
char temp;
while (begin < end)
{
temp = *begin;
*begin++ = *end;
*end-- = temp;
}
}

int main()
{
char s[] = "i like this program very much";
char *temp = s;
reverseWords(s);
printf("%s", s);
getchar();
return 0;
}
Output:

much very program this like i
The above code doesn’t handle the cases when the string starts with space. The following version handles this specific case and doesn’t make unnecessary calls to reverse function in the case of multiple space in between. Thanks to rka143 for providing this version.

void reverseWords(char *s)
{
char *word_begin = NULL;
char *temp = s; /* temp is for word boundry */

while( *temp )
{
if (( word_begin == NULL ) && (*temp != ' ') )
{
word_begin=temp;
}
if(word_begin && ((*(temp+1) == ' ') || (*(temp+1) == '')))
{
reverse(word_begin, temp);
word_begin = NULL;
}
temp++;
}
reverse(s, temp-1);
}
Time Complexity: O(n)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote