Tiburcio
Tiburcio

Reputation: 25

Detect if string ends with 'A' assembly (HLA) program

Im trying to figure out why my program doesn't output 1 if the string ends with an A. For example this is what it outputs

Please enter a string to process
sfrA
----> here is the string you entered: sfrA
Please enter a string to process
gtf
----> here is the string you entered: gtf

Instead it should out put

Please enter a string to process
sfrA
after endsWithA --- result= 1

I don't see a part of my code working in the output and I also get a memory access violation error after I inputted gtf. Any help is greatly appreciated.

This is my current code so far

program StringProgram;
#include( "stdlib.hhf" );
#include( "cs17string.hla" ); // allows use of gets and puts

static
  stringData : dword;
  answer : int32;

procedure endsWithA( stringData : dword ); @nodisplay; @noframe;
static
dReturnAddress : dword;
begin endsWithA;
// Preserve registers
push( EAX );
push( EBX );
push( ECX );
push( EDX );

// Get the return address off the stack
pop( dReturnAddress );

// Get stringData off the stack
mov( stringData, EAX );

// Calculate the length of the string
mov( EAX, EBX );
mov( EAX, ECX );

// Check if the string is empty
cmp( ECX, 0 );
je no_end_with_a;

// Get the last character
dec( ECX );
add( EBX, ECX );
mov( [EBX], AL );

// Compare the last character with 'A' and 'a'
cmp( AL, 'A' );
je end_with_a;
cmp( AL, 'a' );
je end_with_a;

no_end_with_a:
mov( 0, EAX );
jmp done;

end_with_a:
mov( 1, EAX );

done:
// Restore the registers used
pop( EDX );
pop( ECX );
pop( EBX );
pop( EAX );

// Push back the return address
push( dReturnAddress );

// Return from function
ret();

end endsWithA;

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

// Pass the string parameter to the function
mov( stringData, EAX );
call endsWithA;
mov( EAX, answer );

// Show the results
stdout.put( "after endsWithA --- result=" );
stdout.put( answer );
stdout.newln();

end StringProgram;

Upvotes: 0

Views: 73

Answers (1)

seannn
seannn

Reputation: 1

You have to use pop( stringData, EAX ); instead of mov( stringData, EAX ); to get the stringData off the stack. Also, you have to obtain the characters in stringData using [] and index. For example,

mov(0, CX);
mov(index, EDX); 
myLoop:
mov([EBX+EDX], CL);
cmp(CL, CH);
je Done;
inc(EDX);
jmp myLoop;
Done:

This loop iterates through the characters in the string saved in EBX until it ends (reaches 0, null character). Hope this helps!

Upvotes: -1

Related Questions