×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Where is the error in the following code?

Where is the error in the following code?

Where is the error in the following code?

(OP)
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?

RE: Where is the error in the following 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.

RE: Where is the error in the following code?

(OP)
Thank you.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close