How 16 bit program recognize 32 bit REG data transaction?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
osdevkid
Member
Member
Posts: 72
Joined: Sun Nov 21, 2010 11:15 am
Location: India, Chennai

How 16 bit program recognize 32 bit REG data transaction?

Post by osdevkid »

Dear All,

How 16 bit programs are works fine with 32 bit register (EBP ESP etc) ?
For example,

Code: Select all

	push ebp			; save EBP reg value
	mov ebp, esp		; move ESP reg value in to EBP reg

	mov esp, ebp		; restore ESP reg value from EBP reg
	pop ebp				; restore EBP reg value
How it recognize 32 bit transactions ?

Some more information about environment:
Code complied with NASM compiler
nasm main.asm -o main.com
Code started with
BITS 16
[ORG 0]
Last edited by osdevkid on Thu Dec 16, 2010 5:30 am, edited 2 times in total.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: How 16 bit program recognize 32 bit REG data transaction

Post by Combuster »

How does it recognise the difference between add and jmp instructions? The difference between ECX and EDX?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
osdevkid
Member
Member
Posts: 72
Joined: Sun Nov 21, 2010 11:15 am
Location: India, Chennai

Re: How 16 bit program recognize 32 bit REG data transaction

Post by osdevkid »

Combuster wrote:How does it recognise the difference between add and jmp instructions?
add & jmp instructions are having different opcode values, machine instruction binary values are different.

My question is,

if I am running 16 bit program on 16 bit processor, then ESP, EBP are not available, instead SP and BP are available, whether it will truncate the higher order bytes or any possibility for system fault exception?

if I am running 16 bit program on 32 bit processor, where they will use whole 4 bytes of ESP, EBP or only lower order bytes ?

NASM how it accepts 32 bit registers in its program starts with
BITS 16
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: How 16 bit program recognize 32 bit REG data transaction

Post by JamesM »

if I am running 16 bit program on 16 bit processor, then ESP, EBP are not available, instead SP and BP are available, whether it will truncate the higher order bytes or any possibility for system fault exception?
The 8086 series were not forwards compatible.
User avatar
osdevkid
Member
Member
Posts: 72
Joined: Sun Nov 21, 2010 11:15 am
Location: India, Chennai

Re: How 16 bit program recognize 32 bit REG data transaction

Post by osdevkid »

JamesM wrote:
if I am running 16 bit program on 16 bit processor, then ESP, EBP are not available, instead SP and BP are available, whether it will truncate the higher order bytes or any possibility for system fault exception?
The 8086 series were not forwards compatible.
So you mean it will truncate the higher order bytes of ESP, EBP etc. In this case, why NASM not given an error or warning when we use 32 bit instructions ?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: How 16 bit program recognize 32 bit REG data transaction

Post by JamesM »

osdevkid wrote:
JamesM wrote:
if I am running 16 bit program on 16 bit processor, then ESP, EBP are not available, instead SP and BP are available, whether it will truncate the higher order bytes or any possibility for system fault exception?
The 8086 series were not forwards compatible.
So you mean it will truncate the higher order bytes of ESP, EBP etc. In this case, why NASM not given an error or warning when we use 32 bit instructions ?
No, as in, it's not compatible. AFAIK the 8086 will bork on 32-bit instructions.
User avatar
osdevkid
Member
Member
Posts: 72
Joined: Sun Nov 21, 2010 11:15 am
Location: India, Chennai

Re: How 16 bit program recognize 32 bit REG data transaction

Post by osdevkid »

AFAIK the 8086 will bork on 32-bit instructions.
=
As Far As I Know, the 8086 will crash on 32-bit instructions
Is it right ?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: How 16 bit program recognize 32 bit REG data transaction

Post by JamesM »

osdevkid wrote:
AFAIK the 8086 will bork on 32-bit instructions.
=
As Far As I Know, the 8086 will crash on 32-bit instructions
Is it right ?
Correct. That is, if you're running on a 16-bit machine (like you mentioned). If you're running on a 32 or 64-bit machine, just in 16-bit mode, it will work.

There is an operand-size override prefix that is put before the instruction to make it 32-bit.
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: How 16 bit program recognize 32 bit REG data transaction

Post by qw »

The [url=http://pdos.csail.mit.edu/6.828/2010/readings/i386.pdf]Intel 80386 Programmer's Reference Manual[/url] wrote:Unlike the 8086 and 80286, 32-bit effective addresses can be generated (via the address-size prefix) [...] Instructions can utilize 32-bit operands through the use of the operand size prefix.
Note that the opposite is also true: when running in 32-bit mode, 16-bit addresses and operands may be used with the same prefixes.
robos
Member
Member
Posts: 33
Joined: Sun Apr 06, 2008 7:04 pm
Location: Southern California

Re: How 16 bit program recognize 32 bit REG data transaction

Post by robos »

Because nasm defaults to a modern processor. And modern processors have 32-bit registers available in 16-bit mode, otherwise you wouldn't be able to switch to protected mode etc.

If you want to restrict which instructions and operands you can use, tell nasm which CPU the code is restricted to / will be running on, example:

Code: Select all

CPU		8086
All of this is in the nasm manual....
- Rob
Post Reply