Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PROGRAM 1 4 : strlen Function Write an HLA Assembly language program that implements the following function: procedure strlen ( baseStringAddress: dword ) ; @nodisplay;

PROGRAM 14: strlen Function
Write an HLA Assembly language program that implements the following function:
procedure strlen( baseStringAddress: dword ); @nodisplay; @noframe;
This function should return in AL the number of characters in the string parameter, passed by its base address. Here is a sample program dialogue:
Feed Me: asdfasdf
The String You Entered: asdfasdf Has Length =8
Feed Me: asdf
The String You Entered: asdf Has Length =4
This is what I have so far:
// File: strlenProgram.hla
program strlenProgram;
#include( "stdlib.hhf");
#include("cs17string.hla" );
procedure strlen( baseStringAddress: dword ); @nodisplay; @noframe;
static
dStringLength : int8;
begin strlen;
// Initialize the string length to 0
mov(0, dStringLength );
// Loop through the string until null character is found
strlenLoop:
mov([baseStringAddress], AL ); // Load the current character
cmp( AL,0); // Compare it with null character
je strlenEnd; // If null, end the loop
inc( dStringLength ); // Increment the string length
inc( baseStringAddress ); // Move to the next character
jmp strlenLoop; // Repeat the loop
strlenEnd:
mov( dStringLength, AL ); // Return the string length in AL
ret();
end strlen;
const
nullchar : byte :=0;
static
inputBuffer: byte[128]; // Buffer to hold user input
stringData : byte[128]; // Buffer for string data (initially empty)
stringDataLen : uns16 :=127; // Max number of chars the string can hold (excluding NULL)
stringLength: byte;
begin strlenProgram;
// Prompt the user to enter some text
stdout.put( "Please enter some text to work with and hit Return!", nl );
// Read input into stringData using gets
// Load effective address of stringData into EAX
mov(&inputBuffer,eax);
push( EAX );
push( stringDataLen );
call gets;
// Call the strlen function to calculate the string length
lea( EAX, stringData ); // Load effective address of stringData into EAX
push( EAX );
call strlen;
// Store the returned length
mov( AL, stringLength );
// Display the result
stdout.put( "The String You Entered: ", stringData, " Has Length =", stringLength, nl );
end strlenProgram;
Can you fix this? What can I do to make it work?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning C# 5.0 Databases

Authors: Vidya Vrat Agarwal

2nd Edition

1430242604, 978-1430242604

Students also viewed these Databases questions