// File: cs17string.hla
// Provides the string manipulation functions puts and gets for
students to use
// this procedure prompts for a string, writing atmost maxLength
bytes into a previously declared array
// this maxLength should not include the terminating null in its
count
// the string parameter is being passed by its base address
procedure gets( baseStringAddress: dword; maxLength : uns16 );
@nodisplay; @noframe;
// uses the register DX, DI, ECX, EBX and EAX
static
dReturnAddress : dword;
wDIRegister : word := 0; // preserve DI
wDXRegister : word := 0; // preserve DX
dEAXRegister : dword := 0; // preserve EAX
dEBXRegister : dword := 0; // preserve EBX
dECXRegister : dword := 0; // preserve ECX
// I am trying to hide the datatype string for use by CS 17
sData : string;
begin gets;
// entry sequence
// preserve registers
mov( EBX, dEBXRegister );
mov( EAX, dEAXRegister );
mov( DX, wDXRegister );
mov( DI, wDIRegister );
pop( dReturnAddress ); // This is the return address
pop( DI ); // This is the max length able to read
pop( EBX ); // This is the base address of the string
// push back the return address
push( dReturnAddress );
// preserve registers
push( wDIRegister );
push( wDXRegister );
push( dECXRegister );
push( dEBXRegister );
push( dEAXRegister );
// begin sub-task
// prompt for a string
stdin.flushInput();
stdin.a_gets(); // allocate and read string into EAX
mov( EAX, sData ); // save address so it can be free'd later
// copy data in sData over into starting at [EBX]
mov( 0, DX ); // EBX will be the address of string
mov( 0, ECX );
getsRepeatLoop:
// read no more than DI chars
cmp( DI, 0 );
je getsEndLoop;
cmp( [ EAX + ECX ], DH );
je getsEndLoop;
mov( [ EAX + ECX ], DL ); // move character desired
mov( DL, (type char [ EBX ]) );
inc( ECX );
inc( EBX );
dec( DI );
jmp getsRepeatLoop;
getsEndLoop:
// set ending null
mov( DH, (type char [ EBX ]) );
// release sData
strfree( sData );
// exit sequence
// restore registers
pop( EAX );
pop( EBX );
pop( ECX );
pop( DX );
pop( DI );
// transfer control
ret( );
end gets;
// this procedure prints the contents of a null-terminated
string
// the string parameter is being passed by its base address
procedure puts( baseStringAddress: dword ); @nodisplay;
@noframe;
// uses the register EBX
static
dReturnAddress : dword;
wDXRegister : word := 0; // preserve DX
dEBXRegister : dword := 0; // preserve EBX
begin puts;
// entry sequence
// preserve registers
mov( EBX, dEBXRegister );
mov( DX, wDXRegister );
pop( dReturnAddress ); // This is the return address
pop( EBX ); // This is the base address of the string
// push back the return address
push( dReturnAddress );
// preserve registers
push( dEBXRegister );
push( wDXRegister );
// begin sub-task
// print null-terminated string
mov( 0, DX ); // EBX will be the address of string
putsRepeatLoop:
cmp( [ EBX ], DH );
je putsEndLoop;
stdout.putc( [ EBX ] );
inc( EBX );
jmp putsRepeatLoop;
putsEndLoop:
// exit sequence
// restore registers
pop( DX );
pop( EBX );
// transfer control
ret( );
end puts;
Create an HLA function that loops through a single string
argument and verifies that there are exactly two z's in the string.
This function should have the following signature:
procedure hasTwozs( stringData : dword ); @nodisplay;
@noframe;
This function should return into EAX a boolean value which is
either true or false, 1 or 0. To receive full credit, your
hasTwozs( ) function must not allocate any storage.
You must use the utility functions gets and puts provided here
Download here by downloading this file. These are the some of the
same routines you used in Unit 15. Once you acquire the file, unzip
it and work with the .hla file you find inside. You can include it
in your code by saying: #include( "cs17string.hla" );
Your function should replicate the following C code:
bool hasTwozs( char * stringData )
{
int i = 0;
int countzs = 0;
while ( stringData[ i ] != NULL )
{
char letter = stringData[ i ];
if (letter == 'z')
countzs = countzs + 1;
i = i + 1;
}
return( countzs == 2 );
}
IN ORDER TO RECEIVE FULL CREDIT, YOU MUST USE THE TEMPLATE
SOLUTION SHOWN BELOW. Of course, you will need to add code to the
function to implement the desired algorithm explained above. In
addition, you will need to push the parameters to the function.
*/
// Students must use this template as the basis for their
solution.
// Please look at the two TODO: notes below
program StringProgram;
#include( "stdlib.hhf" );
// The file cs17string.hla is downloadable from the hyperlink
shown above.
// Place it in the same folder as this hla file you are working
on
#include( "cs17string.hla" );
static
stringData : dword;
answer : int32;
// TODO: Students add code below to implement this function
// Several hints are supplied
procedure hasTwozs( stringData : dword ); @nodisplay;
@noframe;
static
dReturnAddress : dword;
begin hasTwozs;
// TODO: Students will need to preserve registers
pop( dReturnAddress );
// push back the return address
push( dReturnAddress );
// preserve registers
push( stringData );
// begin function implementation
// leave the count in EAX
// restore the registers used
ret();
end hasTwozs;
begin StringProgram;
stdout.put( "Please enter a string to process", nl );
// this code allocates a string of size 80
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, EAX );
push( EAX );
mov( 80, CX );
push( CX );
call gets;
// print the string
stdout.put( "----> here is the string you entered: " );
mov( stringData, EAX );
push( EAX );
call puts;
stdout.newln();
// initialize EAX before calling the function.
mov( 0, EAX );
// TODO: send a string parameter to the function
call hasTwozs;
mov( EAX, answer );
// show the results
stdout.put( "after hasTwozs --- result=" );
stdout.put( answer );
stdout.newln();
end StringProgram;
// File: cs17string.hla // Provides the string manipulation functions puts and gets for students to use // this procedur
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am