Author Topic: Pointing a data with a general register  (Read 13133 times)

Offline nasmuser

  • Jr. Member
  • *
  • Posts: 6
Pointing a data with a general register
« on: April 22, 2012, 01:36:16 PM »
Hello everybody !

I've done a code which doens't want to be assembled. The code's here :

Code: [Select]
org 100h

Initialisation:
    XOR  BH,BH
    MOV  CX,1
    MOV  AH,0x0A
    MOV  DX,texte

Traitement:
     MOV  AL,[DX]
     OR   AL,AL
     JZ   Sortie
     INT  0x10
     INC  DX
     JMP  Traitement

Sortie:
     ret

texte: db "Hello, World !",0

The goal is to print a message in the console without index registers but using the bios routine.
But the assembler gives an error : "invalid effective adress" line 11. The line 10 is :
Code: [Select]
     MOV  AL,[DX]
Thank you !

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: Pointing a data with a general register
« Reply #1 on: April 22, 2012, 01:56:30 PM »
Read x86 Addressing Modes @ Wikipedia for a summary of what you can do with the various x86 addressing modes.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Pointing a data with a general register
« Reply #2 on: April 22, 2012, 02:24:36 PM »
Well, Nasm is correct - that's not a valid instruction! Using 16-bit instructions, an effective address contains an (optional) offset, plus an (optional) base register (bx and bp), plus an (optional) index register (si and di). That's it!

Using 32-bit instructions, any GP register can be used as a base register and any but esp can be used as an index register. A "scale" can also be used, such as:
Code: [Select]
mov al, [ecx + 4 * edx]

In your code, it would "probably" work to do:
Code: [Select]
mov al, [edx]
... provided that the upper bits of edx are zero - "probably" true, but it might be a good idea to explicitly clear 'em!

More "usual" 16-bit code would use bx, si, or di in this position. (If you use si, the single-byte instruction "lodsb" will load al, and increment si.)

I'd have to look it up, but I think you want 0xE in ah for this interrupt, not 0xA. If my memory is correct (quite possibly not!), 0xA does not advance the cursor, and everything will print in the same position... unless you explicitly advance the cursor after each character.

Get back to us if you have trouble getting it working.

Best,
Frank


Offline nasmuser

  • Jr. Member
  • *
  • Posts: 6
Re: Pointing a data with a general register
« Reply #3 on: April 22, 2012, 02:45:50 PM »
Thank's a lot for twice and quick answers ! The link seems to be very usefull.
For the cursor you're right Frank Kotler. But it's possible to move ourselves the cursor.

The code now works with the EAX register. But is it possible to use it with all modes ? (real, protected...)

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Pointing a data with a general register
« Reply #4 on: April 22, 2012, 03:37:42 PM »
Right, we can advance the cursor. You might also want to look at int 0x10, ah - 13h:

http://www.ctyme.com/intr/rb-0210.htm

The "rules" are quite different in Real Mode vs Protected Mode. I think the link Keith gave you explains it pretty well. Protected Mode usually implies 32-bit code, and the BIOS interrupts are 16-bit code, so int 0x10 won't work! The valid addressing modes depend on 16-bit vs 32-bit instructions, so should work in either Real Mode or Protected Mode - but you need to be careful not to exceed the "limit" with the entire effective address, usually 0xFFFF in Real Mode. I'm not sure exactly what you're asking here.

Best,
Frank


Offline nasmuser

  • Jr. Member
  • *
  • Posts: 6
Re: Pointing a data with a general register
« Reply #5 on: April 25, 2012, 07:29:04 AM »
Thank's a lot for the link.
You didn't know exactly what I asked but you've perfectly answered to my question !
If I understood : using 32-bit registers in real mode works if using only the 16 least significant bits of them.

Despite the number of members, this forum is really cool ! The answers are perfect and very quick.

PS : I'm french and have a bad english, so if you meet an error, you can say it to me. ;)

@+