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

!!!Must be HLA please!!! Create an HLA function that returns a integer value bas

ID: 3668165 • Letter: #

Question

!!!Must be HLA please!!!

Create an HLA function that returns a integer value based on a single string argument and a single letter. This function should have the following signature:

procedure replaceAll( stringData : dword; letter : byte ); @nodisplay; @noframe;

This function should alter the string parameter, replacing each letter of the string with the passed character parameter, and return a count of how many letters were changed. To receive full credit, your replaceAll() procedure must not allocate any storage.

You are free to use the utility functions provided here by downloading this file. These are the some of the same routines you used in Unit 15. If you look them over, you might recognize some useful routines for the problem above. Whether you use them is completely up to you...

Also, your function should replicate the following C code:

int replaceAll( char * stringData, char letter ) {

int i = 0;

int result = 0;

while ( stringData[ i ] != NULL ) {

if (stringData[ i ] != letter) {

    stringData[ i ] = letter;
    result = result + 1;
}
i = i + 1;

}
return( result );

}

To assist you with this task, I am offering you the following program template to use to create your solution. Of course, you will need to add code to the function to implement the desired algorithm explained above. In addition, you will need to prepare and push the parameters to the function.

// String Parameter Template Solution For CS 17 Final
// CS 17 Students must use this template as the basis for their solution.
// I hope it will help simplify your development task.

// Please look at the two TODO: notes below

program StringProgram;

#include( "stdlib.hhf" );
// The file cs17Final.hla is downloadable from the hyperlink shown above.
// Place it in the same folder as this hla file you are working on.

#include( "cs17Final.hla" );
static
stringData : dword;

// TODO: CS 17 Students add code to implement this function
// Several hints are supplied
procedure replaceAll( stringData : dword; letter : byte ); @nodisplay; @noframe;
static
dReturnAddress : dword;
begin replaceAll;

// entry sequence
// preserve registers used
pop( dReturnAddress ); // this is the return address

// push back the return address
push( dReturnAddress );
// preserve registers

// begin sub-task

// restore the registers used
ret();
end replaceAll;

begin StringProgram;
stdout.put( "Please enter a string to process", nl );
// this code prompts for a length and allocates a string that size
mov( @size( int8 ), AL );
mov( 80, BL );
inc( BL );
mul( BL );
mov( 0, EBX );
mov( AX, BX );
malloc( EBX );
mov( EAX, stringData );

// let's try reading a value into the string
mov( stringData, EBX );
push( EBX );
mov( 80, CX );
push( CX );
call gets;

// print the string
stdout.put( "----> here is the string you entered: " );
mov( stringData, EBX );
push( EBX );
call puts;
stdout.newln();

// initialize EAX before calling the function.
mov( 0, EAX );
// TODO: send a string parameter to the function
// TODO: send a letter parameter to the function
// TODO: call replaceAll

// show the results
stdout.put( "count returned from replaceAll call=" );
stdout.put( EAX );
stdout.newln();

!!!Must be HLA please!!!
end StringProgram;

Explanation / Answer

Consider the following


procedure win32cons.gets( y:dword; x:dword; len:dword; s:string ); @nodisplay;
var
   csbi:        win.CONSOLE_SCREEN_BUFFER_INFO;
   charsRead:   dword;
   xyposn:       win.COORD;
  
begin gets;

   pushad();
  

   // Verify that the coordinates are within range:
  
   win32cons.info( csbi );
   if
   (#{
       mov( y, eax );
       cmp( ax, csbi.dwSize.Y );
       jae true;
       mov( x, eax );
       add( len, eax );
       cmp( ax, csbi.dwSize.X );
       jb false;
   }#) then

       raise( ex.ValueOutOfRange );
      
   endif;


// Verify that the string is big enough:
  
   mov( len, eax );
   mov( s, ebx );
   if( eax > (type str.strRec [ebx]).MaxStrLen ) then
  
       raise( ex.StringOverflow );
      
   endif;
  
   // Set the length of the resulting string and zero terminate it:
  
   mov( eax, (type str.strRec [ebx]).length );
   mov( 0, (type byte [ebx+eax]) );
  
   // Set up the coordinates where we will grab the string.
  
   mov( y, ecx );
   mov( cx, xyposn.Y );
   mov( x, ecx );
   mov( cx, xyposn.X );
  
   // Read the string data from the screen:
  
   win.ReadConsoleOutputCharacter
   (
       charsRead,
       xyposn,
       eax,
       (type byte [ebx]),
       stdout.handle()
   );
   popad();
  
end gets;
     

procedure win32cons.a_gets( y:dword; x:dword; len:dword ); @nodisplay;
var
   csbi:        win.CONSOLE_SCREEN_BUFFER_INFO;
   charsRead:   dword;
   xyposn:       win.COORD;
   theString:   dword;
  
begin a_gets;

   pushad();
  

   // Verify that the coordinates are within range:
  
   win32cons.info( csbi );
   if
   (#{
       mov( y, eax );
       cmp( ax, csbi.dwSize.Y );
       jae true;
       mov( x, eax );
       add( len, eax );
       cmp( ax, csbi.dwSize.X );
       jb false;
   }#) then

       raise( ex.ValueOutOfRange );
      
   endif;


   // Allocate storage for the string:
  
   mov( len, eax );
   mov( eax, ebx );
   stralloc( eax );
   mov( eax, theString );
  
   // Set the length of the resulting string and zero terminate it:
  
   mov( ebx, (type str.strRec [eax]).length );
   mov( 0, (type byte [eax+ebx]) );
  
   // Set up the coordinates where we will grab the string.
  
   mov( y, ecx );
   mov( cx, xyposn.Y );
   mov( x, ecx );
   mov( cx, xyposn.X );
  
   // Read the string data from the screen:
  
   win.ReadConsoleOutputCharacter
   (
       charsRead,
       xyposn,
       ebx,
       (type byte [eax]),
       stdout.handle()
   );
   popad();
   mov( theString, eax );
  
end a_gets;

end consoleUnit;