Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Where is the error in the following code? 1

Status
Not open for further replies.

ak24

Programmer
Mar 21, 2011
2
Hello...

I am learning assembly, and am reading "The Art of Assembly" book.

I'm on chapter 4, and I can't figure out where is the error in the following code:

"The last problem with pointers is the lack of type-safe access. This can occur because
HLA cannot and does not enforce pointer type checking. For example, consider the program in Listing 4-7:

// Program to demonstrate use of
// lack of type checking in pointer
// accesses.

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

static
ptr: pointer to char;
cnt: uns32;

begin BadTypePtrDemo;

// Allocate sufficient characters
// to hold a line of text input
// by the user:

malloc( 256 );
mov( eax, ptr );

// Okay, read the text a character
// at a time by the user:

stdout.put( "Enter a line of text: ");
stdin.flushInput();
mov( 0, cnt );
mov( ptr, ebx );
repeat

stdin.getc(); // Read a character from the user.
mov( al, [ebx] ); // Store the character away.
inc( cnt ); // Bump up count of characters.
inc( ebx ); // Point at next position in memory.

until( stdin.eoln());

// Okay, we've read a line of text from the user,
// now display the data:

mov( ptr, ebx );
for( mov( cnt, ecx ); ecx > 0; dec( ecx )) do

mov( [ebx], eax );
stdout.put( "Current value is $", eax, nl );
inc( ebx );

endfor;
free( ptr );

end BadTypePtrDemo;

"This program reads in data from the user as character values and then displays the data as double word hexadecimal values.
While a powerful feature of assembly language is that it lets you ignore data types at will and automatically coerce the data without any effort,
this power is a two-edged sword.

If you make a mistake and access indirect data using the wrong data type, HLA and the 80x86 may not catch the mistake, and your program may produce inaccurate results.
Therefore, you need to take care when using pointers and indirection in your programs that you use the data consistently with respect to data type."

Does anyone know where is the error in the above code?
 
it takes as input bytes, but is outputing in dwords. There is no error per se, but you are not going to get the results you probably expected. For instance, if you entered "A","B","C" and "D", you would get something like the following for output (XX are bytes following the end of the inputed string):

Current value is $44434241
Current value is $13444342
Current value is $0A134443
Current value is $XX0A1244


Actual output may be different as I am not familiar enough with HLA (I use TASM myself) to know how it's library handles EOL and whether it's stdout.put call will automatically convert numeric values to their string representation or not. I am assuming that the getch and put routines act similar to C's.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top