Tuesday 13 October 2009

Console Dev - Week 2 - Machine Cycle

The original Xbox's ISA

Because the Xbox uses an Intel Pentium III processor, its ISA is not very different from what is generally found in computers. The CPU uses the x86 instruction set, which is described in detail in the following link :

x86 instruction listing (Wiki)

The corresponding disassembly code can be obtained using the free software IDA Pro Freeware v4.3 or Ollydbg. However, not having managed to read an original Xbox cd successfully with a computer dvd-rom (Xbox cds are read from the outside->in, whereas normal dvd-rom read from the inside->out), here is a sample code of a strlen function written in x86 assembly code, found on the internet, with added comments to explain what I believe is going on, based on the code, the registers and the instruction set:


section .text
global my_strlen
my_strlen:
push ebp
//pushes the data onto the ebp register ( stack base pointer for current frame )

mov ebp, esp //moves the data from esp (stack base pointer for top frame) into ebp
push ebx //pushes the data onto the base register
push ecx //pushes the data onto the counter register

mov ebx, [ebp+8] //moves the data in the ebp register at location 8 (8 bits?) onto the base register
mov ecx, 0 //sets the counter to zero

.L1:
mov eax, [ebx]
//put the adress of the base register into the accumulator
cmp byte [ebx], 0 //compares the single byte in ebx with zero (checks whether the character is empty or not?)
jz .L2 //"jump on zero", meaning if the comparison is false, go to L2...

inc ebx //otherwise, increment the base register by 1 (go to next character ?)
inc ecx //increment the counter by 1 (one more character in the string)
jmp .L1 //go back to L1 and repeat
.L2:
dec ecx
//decrements the counter by one (correction)
mov eax, ecx //put the result into the accumulator

pop ecx //pop data from the counter (reset it ?)

leave
ret

The code above is not perfect, but it allows us to see how the x86 instructions work, and how the various registers are used. The same principle is also applied, in a much larger scale that is, with the Xbox, with the added compiled C/C++ files on top of the low level assembly code, which then makes it able to run games...

Other sources :

http://www.geocities.com/thestarman3/asm/ - Additional resources for x86 code

http://www.linuxforums.org/forum/linux-programming-scripting/76566-x86-assembly-strlen-implementation.html - Original strlen x86 code

http://en.wikipedia.org/wiki/X86 - more information on x86 concerning registers, etc.

No comments:

Post a Comment