News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

DMC

Started by Harrism, May 28, 2006, 05:52:38 PM

Previous topic - Next topic

Harrism

Hi, I'm not sure if my last topic got moved or a previous backup restored due to the internal server error the otherday.

Anyway, I have just a couple of questions I was wondering if someone could help me with.

I've looked at compiled code, including but not just emu8086, and I haven't managed to work out the laymans terms.

In ADD
Mnemonic : ADD reg/mem16, imm16 -> opcode: 81 /0 iw
Mnemonic : ADD reg/mem16, reg16 -> opcode: 01 /r

I understand what everything else is, but what /0 and /r mean.
regmem16 also is making me think. Is there a tutorial on any of this I should be reading?

Thankyou,

Harry.

I am doing this :-)

Ossa

From Intel Manual 2A Section 3.1.1.1:

Quote

  • /digit — A digit between 0 and 7 indicates that the ModR/M byte of the instruction uses
    only the r/m (register or memory) operand. The reg field contains the digit that provides an
    extension to the instruction's opcode.
  • /r — Indicates that the ModR/M byte of the instruction contains a register operand and an
    r/m operand.

so "81 /0 iw" means that the opcode (single byte: 81h) is followed by the ModR/M byte where the reg field (bits 3-5) are set to 0. The other fields are set as needed. This might be followed by a SIB byte if needed. This byte is followed by a 2 byte immediate value. Therefore

add dx, 6468h

becomes (in hex):

66 81 C2 68 64

since we are in 32-bit mode, we use the operand size override prefix first (66). Then comes the opcode (81). Then the ModR/M byte (C2) then the immediate word (6468) - the bytes are reversed because this is a word value, so they appear the opposite way around in memory.

The ModR/M byte is C2, so in binary: 11000010

Therefore the fields are:

Mod        (6-7):   11
reg/opcode (3-5):  000
R/M        (0-2):  010


Which means that we are addressing DX (from the intel manual's table).

Hope that helps (and that I didn't make any mistakes there),
Ossa

[edit] whoops forgot the memory case... might need the SIB byte [/edit]
[edit] if you can't find a tutorial on this and really want one, I might write one (just had a little google and couldn't find a good one)... but I won't be able to start doing that until wednesday (got to write a 50 page report by then - worth about a quarter of my MEng, so I feel I should put some effort in) [/edit]
Website (very old): ossa.the-wot.co.uk

Roger

Hi Harry,

The second byte for these particular instructions is of the form     mod reg r/m

Tthe /0 and /r refer to the reg part only. The mod and r/m parts don't vary in meaning and so are not mentioned. The reg part can vary so the /0 and /r specify what the reg part does.

The first form 81 /0 iw has an immediate operand and so it does not specify two registers and the reg field is not needed. These bits are actually used to select a particular arithmetic/logical instruction so /1 = OR: /2 = ADC; /3 =  SBB, etc thro AND, SUB, XOR and CMP.
iw is the immediate word.

The second form 01 /r is "Memory or RegesterOperand with Regester Operand" and so the /r tells you that the reg field does its usual job of specifying a particular register.

reg/mem16 is stating that one of the two operands needed may be either the contents of a memory or a register which will be determined by the mod field. The r/m field will then specify which regester or which memory addressing mode.

I don't know of any tutorial on this and as your favorite assembler will work them out for you, I don't think it is a subject you are likely to get a tute for.  (Someone will now prove me wrong :red).

BWT I use an old Intel iAPX 86/88,186188 User's Manual for this but as it is over 20 years old I doubt if you could find a spare copy now.

Regards Roger

Harrism

I think I'm beginning to understand.

Both your example, http://www.logix.cz/michal/doc/i386/chp17-02.htm, and emu8086 seem to be explaining it.

Thanks  :bg

Harrism

hi,

From what you said last time Ossa, and Roger I was able to find and understand the answer I needed. I was wondering, I need another bit of help.

I know there's compatiablity mode, 66h and for 64bit mode the REX 4Xh prefix.

There are 3 operating modes, Pure 64bit mode, Compatibility Mode, and Legacy Mode. How can I choose which one of the three I wish to use? 66h and 4Xh will only know what to do when it knows which operating mode it is in.

If that makes sense.

stanhebben

In legacy mode, you have real mode and protected mode.
In long mode, you have 64-bit mode and compatibility mode.

The modes can be set using certain flags. You also can't switch from any mode to any mode.

The processor starts up in real mode. To go from real to protected (Legacy) mode, set CR0.PE (protection enable). To go from protected to Long mode, set EFER.LME and enable paging. By setting CS.L in long mode, you select 64-bit mode. By clearing it, you select compatibility mode.

CR0 and EFER are system registers, and CS is the code segment register.

You actually have to do more than just setting flags - you also have to set up structures to get these modes working - which is not that simple. The AMD manual, volume 2, chapter 14 contains a good explanation and some great example code. Also don't ask me for all the details ;)

Stan

Ossa

erm... before you go running off to do/read ANY of that, STOP!!!

You cannot access the CRx registers under windows - only a ring0 process can do that - Windows its drivers are ring0 and will do it for you (and your ring3 application). Having said that, do read the references that Stan gave - but remember that the info contained in the systems programming manuals is only useful for OS or driver developers. It is interesting and important to understand the OS, but it is not directly important for programming a windows application.

By writing a 32-bit application, windows will always set you up for 32-bit execution. For 16-bit apps, 16-bit execution... and 64-bit, you guessed it, 64-bit execution. Basically decide if you are writing a 64 or 32 bit application and then code based on that premise. So long as you set up the PE headers correctly, your application will run fine under windows.

If you ARE writing an OS or are writing apps for an OS other than windows, do tell us as when you begin asking about application file formats, we will end up talking corss-purposes as I will assume you are writing 32-bit windows apps (that use the MZ and PE headers - general COFF format).

Ossa
Website (very old): ossa.the-wot.co.uk

Harrism

Will do.

I've taken my time, and I just dropped back to say thankyou, suddenly the ModRM/SIB bytes are far easilier to understand. It took reading about the REX Prefix before I completely understood them this afternoon. (Despite the blazing heat outside)

Yes I am writing an os, and the first thing I need to do is write an assembler for it.

Yes I am crazy.

I'm not too worried about fileformat for now. That comes under the theory which a few of us are getting together to discuss at some point. The only thing I'm concerned about is the assembler, which at the moment will be limited to the AMD architecture.

Changing modes will look like it will be difficult, so I'll have to take some careful thought over it, but thankyou for the reference Stan. I'll enjoy reading it tonight and tomorrow  :bg

As for the OS Ossa, I'll try my best to remember.

Thankyou both of you.

Ossa

OK... OS dev - here are a few links I'll give you (in case you haven't already gone over this in huge detail):

OS Development Forum: http://www.osdever.net/forums/index.php
Mega-Tokyo OS Development FAQ: http://mega-tokyo.com/osfaq2/
Write your own OS: http://my.execpc.com/~geezer/osd/
General Hardware Info: http://inferno.cs.univ-paris8.fr/~am/memo/programming/kernel/hardware/
More Hardware Info: http://www.powernet.co.za/info/hw/Index.Htm

I also have some other files that took some finding (e.g. the PCI spec) - if you are interested, just PM me.

Ossa

PS: Don't forget the A20 line  :bg I did and it screwed me for a while.
Website (very old): ossa.the-wot.co.uk

dsouza123

A page about opcode descriptions,encodings and the x86 instruction set.

http://nasm.sourceforge.net/doc/html/nasmdocb.html