×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

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

Inline Assembly Help Please!

Inline Assembly Help Please!

Inline Assembly Help Please!

(OP)
Hi, I recently started programming with inline assembly (I use Visual C++), but I have found a bug that I cannot solve.
Why does it give an error?

// C language
unsigned char array[] = {1, 2, 3, 4, 5, 6, 7};

// Assembly
__asm {
XOR CL,CL

MOV AL,array[CL]
}

The error is -> "error C2403: 'CL' : register must be base/index in 'second operand'".

I tested it many times and I found that the only way to make it work is to use ECX register as index! Why?
If I use ANY OTHER register it will not compile (as above) or it will give a Fatal Error if I use another EXX register (as EAX or EBX).
The problem is that on my program I have a "nested for loop" and so I can't use just 1 counter (ECX), but I need 2 of them and they must be BYTE register (as CL/CH) because the array variable is a char. Any idea?

Thank you very much!
Luke

P.S: any better idea on how to write effective inline nested loop?

RE: Inline Assembly Help Please!

I'm not totally sure, but you might try to PUSH ECX, go through the inner counter, using ECX, then store ECX in any other register, and POP ECX to return to your previous count.

RE: Inline Assembly Help Please!

  Just so I am sure I am reading this correctly, you want to move the byte at Array[CX] into AL, correct? if so try this:

...
xor bx, bx;          zero out bx register
mov AL, array + bx;  move byte at array[bx] to al
...

as for your second question;

...
mov cx,OuterLoopCount
OuterLoop:
push cx
; do outer loop stuff
mov cx, InnerLoopCount
InnerLoop:
; do inner loop stuff
...
Loop InnerLoop
; do more outer loop stuff
pop cx
loop OuterLoop
...

Works real nice for me

Notes on looping; you need to use the entire cx register if in Real (16 bit) mode or ecx in Protected (32 bit) mode because looping will not end until the appropiate register reaches zero; if you have something you want to save in CH for example, the loop command will continue to decrement/increment thru CH also, thereby destroying what you wanted to save, which is why I used push cx to save the outer loop variable and pop cx to restore it. You could just as easily use another reg to store CX, but then you run the risk of accidently overwriting it.

90% of all questions can be answer by the Help file!  Try it and be amazed.

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