Tiburcio
Tiburcio

Reputation: 25

How can I fix a memory access violation in my assembly (hla) program?

Im trying to see what I'm doing in the code I have so far. The goal of my program is the calculate how characters the user's sting has.

For example

Feed Me: asdf
The String You Entered: asdf Has Length = 4

I've been trying to fix it for quite a while but I can't seem to find much resources on HLA and I'm assuming something is wrong with the stack. Any help is greatly appreciated.

program strlenDemo;
#include("stdlib.hhf");

static
    length: byte;
    inputString: string;

procedure strlen( baseStringAddress: dword ); @nodisplay; @noframe;
begin strlen;
    mov( baseStringAddress, esi );  // Load the base address of the string into ESI
    mov( 0, ecx );                  // Clear ECX, it will be used as the character counter
    
    strlen_loop:
        mov( [esi], al );           // Load the current byte of the string into AL
        test( al, al );             // Test if AL is zero (null character)
        jz strlen_done;             // If zero, jump to strlen_done
        inc( ecx );                 // Increment the character counter
        inc( esi );                 // Move to the next character
        jmp strlen_loop;            // Repeat the loop
        
    strlen_done:
        mov( cl, al );              // Move the character count to AL
end strlen;

begin strlenDemo;
    
    stdout.put("Feed Me: ");
    stdin.gets(inputString);   // Read input string, leave space for null terminator
    
    strlen( inputString );          // Call the strlen function with the address of inputString
    mov( al, length );              // Move the result from AL to the length variable
    
    stdout.put("The String You Entered: ");
    stdout.put(inputString);
    stdout.put(" Has Length = ");
    stdout.puti8(length);
    
end strlenDemo;

Upvotes: 0

Views: 97

Answers (0)

Related Questions