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

Create an HLA function that returns a integer value based on a single string arg

ID: 3668136 • Letter: C

Question

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();

end StringProgram;


Explanation / Answer

#include( "stdlib.hhf" )

const

HelloWorld := "Hello World";

begin SampleHLApgm;

stdout.put( "The classical 'Hello World' program: ", HelloWorld, nl );

end SampleHLApgm;

This program does the obvious thing. Anyone with any high-level language background can probably figure out everything except the purpose of "nl" (which is the newline string imported by the standard library). This certainly doesn't look like an assembly language program; there isn't even a real machine instruction in sight. Of course, this is a trivial example; nonetheless, I've managed to write reasonable HLA programs that were just over 1,000 lines of code that contained only one or two identifiable machine language instructions. If it's possible to do this, how can I get away with calling HLA an assembly language?

The truth is, you can actually write a very similar looking program with MASM. Here's an example I trot out for unbelievers. This code is compilable with MASM (assuming you include the UCR Standard Library v2.0 and some additional code I've cut out for brevity:

var

enum colors,<red,green,blue>

colors c1, c2

endvar

Main proc

mov ax, dseg

mov ds, ax

mov es, ax

MemInit

InitExcept

EnableExcept

finit

try

cout "Enter two colors:"

cin c1, c2

cout "You entered ",c1," and ",c2,nl

.if c1 == red

cout "c1 was red"

.endif

except $Conversion

cout "Conversion error occured",nl

except $Overflow

cout "Overflow error occured",nl

endtry

CleanUpEx

ExitPgm ;DOS macro to quit program.

Main endp

As you can see, the only identifiable machine instructions here are the ones that initialize the segment registers at the beginning of the program (which is unnecessary in a Win32 environment). So allow me to blunt criticism from "die-hard" assembly fans right at the start: HLA doesn't open up all kinds of new programming paradigms that weren't possible before. With some clever macros (e.g., enum, cout, and cin in the MASM code), it is quite possible to do some amazing things. If you're wondering why you should bother with HLA if MASM is so wonderful, don't forget my comments about the robustness of these macros. Both HLA and MASM (with the UCR Standard Library v2.0) work great as long as you write perfect code and don't make any mistakes. However, if you do make mistakes, the MASM macro scheme rapidly gets ugly.

The "die-hard" assembly fan will probably observe that they would never write code like the MASM code I've presented above; they would write traditional assembly code. They want to write traditional code. They don't want this high level syntax forced upon them. Well, HLA doesn't force you to use high-level control structures rather than machine instructions. You can always write the low level code if you prefer it that way. Here is the original HLA program rewritten to use familiar machine instructions:

program SampleHLApgm2;

#include( "stdlib.hhf" )

static

dword 37, 37;

TcHWpStr: dword; @nostorage;

byte "The classical 'Hello World' program: ",0,0,0;

dword 11, 11;

HWstr: dword; @nostorage;

byte "Hello World",0;

begin SampleHLApgm2;

lea( eax, TcHWpStr );

push( eax );

call stdout.puts;

lea( eax, HWstr );

push( eax );

call stdout.puts;

call stdout.newln;

end SampleHLApgm2;

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