Author Topic: How to use ModR/M and Effective Addressing?  (Read 5877 times)

Offline AssemblerX86

  • Jr. Member
  • *
  • Posts: 5
How to use ModR/M and Effective Addressing?
« on: October 13, 2014, 09:15:14 PM »
Hi,

 I have searched the web but I wasn't able to find any clear explanation of how to use ModR/M to convert operands into machine code.

 My problem is with R/M itself and how the stacks are used. I would really appreciate it if someone can explain to me how and what for R/M is used. A brief explanation of stacks (CS, DS, SS etc...) and how to use them would also be great.

Thanks!

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How to use ModR/M and Effective Addressing?
« Reply #1 on: October 13, 2014, 11:59:45 PM »
What the old Nasm manual used to say about the ModR/M byte (and the SIB byte, if any) can be found here:

http://home.myfairpoint.net/fbkotler/nasmdocc.html#section-A.2.5

I don't know how "clear" you'll find it. I find it rather confusing, myself. You might be better off with Intel/AMD manuals, or perhaps Wikipedia has something on it. I just let Nasm worry about it. If you do your own conversion from mnemonics to machine code, you put poor Nasm out of a job. :)

I'm not sure what you mean by "stacks", the way you use it. The registers you mention - CS, DS, SS (and ES, FS, and GS) are segment registers. SS is the stack segment, otherwise they don't have much to do with "stacks". They don't use quite the same instructions as "general purpose registers". In particular, we can't move an immediate value into a segment register, only a general purpose register or memory. That's why you'll see:
Code: [Select]
mov ax, data
mov ds, ax
; or
push cs
pop ds
; mov ds, 0 ; No!
They are interpreted differently in real mode and protected mode. In real mode, the value in the segreg is multiplied by 16 and added to an offset to form an address. In protected mode, the value in the segreg is a "selector" - essentially an index into an array of "descriptors" - structures containing "base", "limit", access rights and some other flags. The base is added to an offset to form an address. In most cases, the base is zero, so the address is just the offset. (FS is an exception - used for "thread local storage")

There's more to it, of course - too much to try to explain here. If you have a more specific question, we might be able to help you more (or maybe not...).

Best,
Frank


Offline AssemblerX86

  • Jr. Member
  • *
  • Posts: 5
Re: How to use ModR/M and Effective Addressing?
« Reply #2 on: October 14, 2014, 11:14:24 AM »
Hi Frank,

 The link you provided is probably the simplest yet the clearest explanation of Effective Address Calculation.

 Thank you for explaining the segmentations too.

 I really wonder if you could send me a PM (Or EMail) with a way to privately contact you. I really hope not to cause any disturb, but I just want a few tips. (I would use PM directly but its disabled in this forum)

Thank you very much!

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How to use ModR/M and Effective Addressing?
« Reply #3 on: October 14, 2014, 01:11:04 PM »
fbkotler(pig's tail)myfairpoint(decimal point)net

I don't know why I bother trying to disguise it - the spammers have found me already.

Feel free to email me privately, but I would prefer to discuss assembly language on the forum where others might learn from us, or teach us, or both.

Best,
Frank