User:New16: Difference between revisions

From OSDev.wiki
Jump to navigation Jump to search
Content deleted Content added
No edit summary
m Bot: Replace deprecated source tag with syntaxhighlight
 
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Opcode Programming]]
Endianness refers to the order in which bytes of a multi-byte structure are placed to main memory (RAM).
The <b>bit</b> order in any byte is normally from bit seven downto bit zero (little-endian) and remains preserved.


Any beginner should start here.
==Big- vs. little-endian==


Before starting assembly or high-level language programming you should know: The processor does only understand opcode. Simply consider this languages named above to be helpful, but they must be interpreted by so called compilers, linkers and assemblers to create the necessary opcode.<br>
In big-endian the most-significant (left-hand) byte, in little-endian the least-significant (right-hand) byte is stored at the lowest address.<br>
The following table shows the byte number of a 32-bit structure with byte three equals bit 31 downto 24, byte two equals bit 23 downto 16, byte one equals bit 15 downto 8 and byte zero equals bit seven downto zero.


So just grab an instance of "<b>Intel® 64 and IA-32 Architectures Software Developer’s Manual</b>", with the subtitle: "<b>Combined Volumes: 1, 2A, 2B, 2C, 3A, 3B, 3C and 3D</b>" (4618 pages in June 2016), and go on with this tutorial.<br>
{| {{wikitable}}
:(Just type the title in the search at "intel.com" .)
|- style="text-align:center" style="background: #FFFFFF"
! Memory<br>address
| style="background: #EEEEEE" |
! <font color="white">' </font> n <font color="white">'</font>
! n+1
! n+2
! n+3
|-
| colspan=7 style="background: #EEEEEE" |
|- style="text-align:center"
! Big-endian:
| style="background: #EEEEEE" |
| 3
| 2
| 1
| 0
|-
| colspan=7 style="background: #EEEEEE" |
|- style="text-align:center; background: #FFFFFF"
! Little-endian:
| style="background: #EEEEEE" |
| 0
| 1
| 2
| 3
|-
|}


==Basics==
Little-endian may seem unlogical until one consider that a variable can be used more easily as 8-bit, 16-bit, 32-bit or even 64-bit value without changing its base address. Therefor AMD and Intel use little-endian order.


You should read at least the following sections (I recommend this order.):
Big-endian is sometimes refered as <b>network byte order</b>. Big endian order is used f.e. in natural languages or .png-files.


*[[Introduction]]
==Examples==
*[[BIOS]]
*[[Boot Sequence]]
*[[Real Mode]]
*[[Assembly]]


You might want to run the following code in an emulator. For windows user I recommend [[VirtualBox]]: Set up a new machine with "Typ: Other" and "Version: Other/Unknown (64-bit)" and make sure you find a floppy-controller under section "Storage".<br>
*Table
You also need a hex-editor (f.e. "https://hexed.it/").<br>
{| {{wikitable}}
After editing and saveing the file, named: "something.img", you should close the hex-editor (-website).
|- style="text-align:center" style="background: #FFFFFF"
! colspan=2 | Memory<br>address
! n
! n+1
! n+2
! ...
|-
| colspan=7 style="background: #EEEEEE" |
|- style="text-align:center"
! rowspan=2 style="background: #F5F5F5" | Value =<br> 0x15
! style="background: #FFFFE0" | Big-endian:
| style="background: #FFFFE0" | 0x15
| style="background: #FFFFE0" | -
| style="background: #FFFFE0" | -
| style="background: #FFFFE0" | -
|- style="text-align:center; background: #FFFFFF"
! Little-endian:
| 0x15
| -
| -
| -
|-
| colspan=7 style="background: #EEEEEE" |
|- style="text-align:center"
! rowspan=2 style="background: #F5F5F5" | Value =<br> 0x0A15
! style="background: #FFFFE0" | Big-endian:
| style="background: #FFFFE0" | 0x0A
| style="background: #FFFFE0" | 0x15
| style="background: #FFFFE0" | -
| style="background: #FFFFE0" | -
|- style="text-align:center; background: #FFFFFF"
! Little-endian:
| 0x15
| 0x0A
| -
| -
|-
| colspan=7 style="background: #EEEEEE" |
|- style="text-align:center"
! rowspan=2 style="background: #F5F5F5" | Value =<br> 0x780A15
! style="background: #FFFFE0" | Big-endian:
| style="background: #FFFFE0" | 0x78
| style="background: #FFFFE0" | 0x0A
| style="background: #FFFFE0" | 0x15
| style="background: #FFFFE0" | -
|- style="text-align:center; background: #FFFFFF"
! Little-endian:
| 0x15
| 0x0A
| 0x78
| -
|-
| colspan=7 style="background: #EEEEEE" |
|-
| colspan=7 style="text-align:center;background: #FFFFFF" | a.s.o.
|-
|}


==First Program==
*Let's assume a x86-computer, which means little-endian order. Register eax contains the value 0xCAFEF00D. You store this register at address 0:


Type in the hex editor (without the leading zero and the "h" at the end):
Memmory address: 0 contains: 0x0D<br>
Memmory address: 1 contains: 0xF0<br>
Memmory address: 2 contains: 0xFE<br>
Memmory address: 3 contains: 0xCA


<syntaxhighlight lang="asm">
Now you read into register ax the value from address 0:


0EBh, 0FEh
Register ax contains: 0xF00D
;now expand this file with zeros until you reached address 510 (01FEh)
0, 0, ...
;at address 510 (01FEh) insert:
0AAh
;at adress 511 (01FFh) insert:
055h
</syntaxhighlight>


Add this file to you floppy controller in your emulator (f.e. VirtualBox).<br>
*Let's assume big-endian order. A 32-bit register contains the value 0xCAFEF00D. You store this register at address 0:
Run your virtual machine.<br>

And you will see: nothing. But you will be able to notice a massive CPU-usage.<br>
Memmory address: 0 contains: 0xCA<br>
It worked! Whatever you have done...<br>
Memmory address: 1 contains: 0xFE<br>
====Explanation====
Memmory address: 2 contains: 0xF0<br>
Lets start from the beginning, 0xEB and 0xFE is a JMP opcode which makes the computer execute this code over and over.<br>
Memmory address: 3 contains: 0x0D
The last 2 bytes are the boot signature, for the computer to understand that this floppy is a bootable floppy disk.

Now you read into a 16-bit register the value from address 0:

This 16-bit register contains: 0xCAFE<br>
(Which is the upper-half of the value. You probably wanted the lower-half and therefor must change the base address and read from address: 2.)

*578 Pound Sterling<br>
"byte" from 9 to 0:<br>
- big-endian: 578 £<br>
- little-endian: 875 £<br>
"byte" from 99 to 0:<br>
- big-endian: 578 £<br>
- little-endian: 785 £

==Common CPUs==

{| {{wikitable}}
|-
! CPU
! Endianness
|-
| x86
| little
|-
| x86-64
| little
|-
| MIPS
| both
|-
| Motorola
| big
|-
| 68k
| big
|-
| PowerPC
| both
|-
|}

Latest revision as of 06:49, 9 June 2024

Opcode Programming

Any beginner should start here.

Before starting assembly or high-level language programming you should know: The processor does only understand opcode. Simply consider this languages named above to be helpful, but they must be interpreted by so called compilers, linkers and assemblers to create the necessary opcode.

So just grab an instance of "Intel® 64 and IA-32 Architectures Software Developer’s Manual", with the subtitle: "Combined Volumes: 1, 2A, 2B, 2C, 3A, 3B, 3C and 3D" (4618 pages in June 2016), and go on with this tutorial.

(Just type the title in the search at "intel.com" .)

Basics

You should read at least the following sections (I recommend this order.):

You might want to run the following code in an emulator. For windows user I recommend VirtualBox: Set up a new machine with "Typ: Other" and "Version: Other/Unknown (64-bit)" and make sure you find a floppy-controller under section "Storage".
You also need a hex-editor (f.e. "https://hexed.it/").
After editing and saveing the file, named: "something.img", you should close the hex-editor (-website).

First Program

Type in the hex editor (without the leading zero and the "h" at the end):

0EBh, 0FEh
;now expand this file with zeros until you reached address 510 (01FEh)
0, 0, ...
;at address 510 (01FEh) insert:
0AAh
;at adress 511 (01FFh) insert:
055h

Add this file to you floppy controller in your emulator (f.e. VirtualBox).
Run your virtual machine.
And you will see: nothing. But you will be able to notice a massive CPU-usage.
It worked! Whatever you have done...

Explanation

Lets start from the beginning, 0xEB and 0xFE is a JMP opcode which makes the computer execute this code over and over.
The last 2 bytes are the boot signature, for the computer to understand that this floppy is a bootable floppy disk.