Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write an HLA Assembly language program that implements the following function: procedure strlen ( baseStringAddress: dword ) ; @nodisplay; @noframe; This function should return in

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
THERE IS AN IMPLEMANTION CODE FILE THAT IS TO BEE ADDED TO THE ACTUAL PROGRAM AND THAT IS: "cs17string.hla" file which implements string i/o routines. "gets" is used to read a string, while "puts" is used to write a string. These routines support the following syntax:
gets( baseStringAddress : dword; maxLength : uns16);
puts( baseStringAddress : dword );
Each routine needs to be passed a nicely allocated and null-terminated string by sending its base address (that is, index 0 of the array). In addition, gets takes a second parameter which tells it how many characters it can legally ramm into the array before it overwrites its allocated size.
Both of these routines are provided in the file: cs17string.hla. By stating in your program,
#include("cs17string.hla"); PLEASE MAKE SURE THE CODE YOU PROVIDE COMPILES AND RUNS WITH NO ERROR. I WILL ALSO BE PROVIDING THE CODE OF THE "cs17string.hla".
// 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[i]
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[i]
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;

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_2

Step: 3

blur-text-image_3

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

Learning PostgreSQL

Authors: Salahaldin Juba, Achim Vannahme, Andrey Volkov

1st Edition

178398919X, 9781783989195

More Books

Students also viewed these Databases questions