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?

Probably line 31? You are trying to move a DWORD into a BYTE... in MASM it would be:

mov    al, BYTE PTR [ebx]

in this way the assembler knows we only want to move a byte from ebx to al.

Wait, HLA is backwards right? you are moving from al to ebx right? either way, BYTE PTR should be correct.

Thank you!

I've been dealing with this code for the last two days and
couldn't figure out where the error was.

This question has already been answered. Start a new discussion instead.