Sunday, November 13, 2011
Procedure and Process
Procedure is a series of steps whereas process is far richer in governance and oversight. Process has feedback mechanism for improvement and is more adaptable to change conditions. A process also interface to other processes.
Tuesday, November 8, 2011
AT&T Mnemonic
When gcc compiles a c program, it actually generate an assembly soruce code in AT&T mnemonics. The output .s file is then assembled using the gas assembler into object module.
General rules for AT&T mnemonics:
(1) All mnemonics and registers are in lower case.
(2) Register names are prefixed by "%"
(3) Every AT&T instruction mnemonics that has operands has a single character suffix indicating the length of the operands. The suffix are b, w, l(long 32-bit). For example, movw %ax,%bx.
(4) Source and destination operands are placed in opposite order from Intel syntax.
(5) Immediate value are prefix by "$". For example, pushl $32
(6) gcc does not generate all AT&T mnemonics.
(7) Displacements in memory references are signed quantities placed outside parantheses containing the base, index and scale values. For example, movl -4(%ebx,%edi) is equivalent to Intel mov byte easx,[ebx+edi-4]
General rules for AT&T mnemonics:
(1) All mnemonics and registers are in lower case.
(2) Register names are prefixed by "%"
(3) Every AT&T instruction mnemonics that has operands has a single character suffix indicating the length of the operands. The suffix are b, w, l(long 32-bit). For example, movw %ax,%bx.
(4) Source and destination operands are placed in opposite order from Intel syntax.
(5) Immediate value are prefix by "$". For example, pushl $32
(6) gcc does not generate all AT&T mnemonics.
(7) Displacements in memory references are signed quantities placed outside parantheses containing the base, index and scale values. For example, movl -4(%ebx,%edi) is equivalent to Intel mov byte easx,[ebx+edi-4]
Linking to the Standard C Library
C runtime library dictates a structure to any programs that link to it. This include a block of code that runs before your program begins and another block of code after your program ends. Your program is called by the startup code as though it is a procedure with the CALL instruction. Your program returns to C library using the RET instruction.
The C calling convention is as follow:
(1) A procedure must preserve the values of the EBX, ESP, EBP, ESI and EDI. The other GPR can be altered.
(2) A procedure's return value in EAX for 32-bit or less. 64-bit return value is stored in EDX and EAX.
(3) Parameters passed to procedures are pushed onto the stack in reverse order. In other words, given MyFunc(p1, p2, p3), p3 is pushed onto stack first, then followed by p2 and p1.
(4) Parameters are removed by the caller after the procedure returns.
(5) The label name of the the assembly program (or the one generated by gcc) must be called "main:" and must be declared as GLOBAL.
The C calling convention is as follow:
(1) A procedure must preserve the values of the EBX, ESP, EBP, ESI and EDI. The other GPR can be altered.
(2) A procedure's return value in EAX for 32-bit or less. 64-bit return value is stored in EDX and EAX.
(3) Parameters passed to procedures are pushed onto the stack in reverse order. In other words, given MyFunc(p1, p2, p3), p3 is pushed onto stack first, then followed by p2 and p1.
(4) Parameters are removed by the caller after the procedure returns.
(5) The label name of the the assembly program (or the one generated by gcc) must be called "main:" and must be declared as GLOBAL.
Monday, November 7, 2011
x86 Jump
There are 2 categories of conditional jump. The problem arises because NASM can generate a binary opcode in different way. A jump target that lies within 127 bytes of the conditional jump instruction is called short jump. A jump that is farther than 127 bytes but within the code segment is called near jump. A near jump can be as far as 2GB away. A far jump go out of a segment which mean specifying both segment and offset. This is quite uncommon.
A short jump generates a short and compact opcode which is always 2 bytes in size. Near jump generates a opcode of either 4 bytes or 6 bytes. NASM generates short jump by default as this is most commonly use in programs. To generate near jump, one must explicitly specify
jne loop ;short jump
jne near loop ;near jump
The above does not apply to unconditional jump.
A short jump generates a short and compact opcode which is always 2 bytes in size. Near jump generates a opcode of either 4 bytes or 6 bytes. NASM generates short jump by default as this is most commonly use in programs. To generate near jump, one must explicitly specify
jne loop ;short jump
jne near loop ;near jump
The above does not apply to unconditional jump.
Linker
There is no reason why assembler cannot generate an executable program as object code file but it is almost never done. The object module cannot be run by itself but requires another step called linking. This model allows breaking big program into smaller parts and each gets written on its own separately.
To process each modules into an executable, linker first builds an index called symbol table, with an entry for every named item in every object module. Once the symbol table is complete, the linker builds an image of how the executable will be arranged in memory when the OS loads it. The image is written to the disk as the executable.
Object modules are allowed to refer to symbols in other object modules using the EXTERN keyword. In the declaring module, the label must be defined using the GLOBAL keyword.
During assembly, these external references are left as holes in the calling module to be filled later. As the linker builds the executable image, it learns where these symbols are in the final image and update them in the reference holes.
Debugging information is a step backward: portions of the soruce code, which was all stripped out early in the assembly process, are put back into the object module by the assembler. These portions of the source code are mostly names of data items and procedures.
Linker also makes the executable relocatable (all address are referenced relative to the start of the executable. This enable the executable to be loaded in any physcial address.
To process each modules into an executable, linker first builds an index called symbol table, with an entry for every named item in every object module. Once the symbol table is complete, the linker builds an image of how the executable will be arranged in memory when the OS loads it. The image is written to the disk as the executable.
Object modules are allowed to refer to symbols in other object modules using the EXTERN keyword. In the declaring module, the label must be defined using the GLOBAL keyword.
During assembly, these external references are left as holes in the calling module to be filled later. As the linker builds the executable image, it learns where these symbols are in the final image and update them in the reference holes.
Debugging information is a step backward: portions of the soruce code, which was all stripped out early in the assembly process, are put back into the object module by the assembler. These portions of the source code are mostly names of data items and procedures.
Linker also makes the executable relocatable (all address are referenced relative to the start of the executable. This enable the executable to be loaded in any physcial address.
x86 Memory Models
Flat Memory Model
The memory is abstracted as a list of consecutive bytes accessed via a linear address. This resemble to the physical address but not necessary when the linear address is used in the translation process under a memory protection model
Segmented Memory Model
Memory is organized in term of distinct region called segment. A byte in segment is referenced by a logical address (also called a far pointer). A logical address comprises 2 parts - a segment selector and an effective address. The logical address can resemble the physical address or may be not depending on the translation process.
IA-32 operates in 3 modes - real mode, protected mode and system management mode (SMM).
SMM
It is used to execute code in firmware (e.g. emergency shutdown, power management)
Real Mode
Real Mode implements a 16-bit execution environment of the old 8086/88. It is an instance of the segmented memory model. The logical address contains 16-bit segment selector and 16-bit effective address.
The selector stores the base address of a 64KB segment. To form a 20-bit address (used by 8086/88), the segment selector is appended with a zero nibble before adding on with the effective address. For example, the logical address 02001010 is translated to 0200[0] + [0]1010 = 03010. The segment address is always at paragraph boundary (because of the appended zero) and the segment is at most 64KB (because of the 16-bit effective address). There is no memory protection under Real Mode.
There are 5 combination of segment registers and GPR:
MSDOS is a real mode OS. The first 640KB is called Conventional Memory. The remaining memory area up to 1MB is called Upper Memory Area, which is reserved for used by ROM and peripheral. Within UMA, there are slot of free memory not used by hardware and accessible by DOS are called UMB (Upper Memory Block). Memory above the real mode 1MB boundary (when 386 was released) are called extended memory.
In a Real Mode Flat Model, program starts at 100h which is a holdover from CP/M-80 (PSP).
Protected Mode
Similar to real mode, protected mode is an instance of segmented memory. The difference is that operating system is now must collaborate with the CPU to resolve addresses. The segment registers are owned by OS and program cannot read or write them.
The memory is abstracted as a list of consecutive bytes accessed via a linear address. This resemble to the physical address but not necessary when the linear address is used in the translation process under a memory protection model
Segmented Memory Model
Memory is organized in term of distinct region called segment. A byte in segment is referenced by a logical address (also called a far pointer). A logical address comprises 2 parts - a segment selector and an effective address. The logical address can resemble the physical address or may be not depending on the translation process.
IA-32 operates in 3 modes - real mode, protected mode and system management mode (SMM).
SMM
It is used to execute code in firmware (e.g. emergency shutdown, power management)
Real Mode
Real Mode implements a 16-bit execution environment of the old 8086/88. It is an instance of the segmented memory model. The logical address contains 16-bit segment selector and 16-bit effective address.
The selector stores the base address of a 64KB segment. To form a 20-bit address (used by 8086/88), the segment selector is appended with a zero nibble before adding on with the effective address. For example, the logical address 02001010 is translated to 0200[0] + [0]1010 = 03010. The segment address is always at paragraph boundary (because of the appended zero) and the segment is at most 64KB (because of the 16-bit effective address). There is no memory protection under Real Mode.
There are 5 combination of segment registers and GPR:
- SS:SP
- SS:BP
- ES:DI
- DS:SI
- CS:IP
MSDOS is a real mode OS. The first 640KB is called Conventional Memory. The remaining memory area up to 1MB is called Upper Memory Area, which is reserved for used by ROM and peripheral. Within UMA, there are slot of free memory not used by hardware and accessible by DOS are called UMB (Upper Memory Block). Memory above the real mode 1MB boundary (when 386 was released) are called extended memory.
In a Real Mode Flat Model, program starts at 100h which is a holdover from CP/M-80 (PSP).
Protected Mode
Similar to real mode, protected mode is an instance of segmented memory. The difference is that operating system is now must collaborate with the CPU to resolve addresses. The segment registers are owned by OS and program cannot read or write them.
x86 general purpose registers
There are 8 GPR. Four of them can be referenced either in full 32-bit or 16-bit.
The 16-bit names are SI, DI, BP and SP. The 32-bit names are prefixed by "E" - ESI. EDI, EBP and ESP.
The other four GPR can be referenced either in 8-bit, 16-bit or 32-bit
32-bit: EAX, EBX, ECX amd EDX
16-bit: AX, BX, CX and DX
8-bit: AH, BH, CH and DH (for the upper 8-bits) and AL, BL, CL and DL (for the lower 8bits).
The 16-bit names are SI, DI, BP and SP. The 32-bit names are prefixed by "E" - ESI. EDI, EBP and ESP.
The other four GPR can be referenced either in 8-bit, 16-bit or 32-bit
32-bit: EAX, EBX, ECX amd EDX
16-bit: AX, BX, CX and DX
8-bit: AH, BH, CH and DH (for the upper 8-bits) and AL, BL, CL and DL (for the lower 8bits).
Subscribe to:
Posts (Atom)