This book begins with a brief overview of CPU instruction code handling, which is still a bit of a mystery after reading it but I will try to describe it to the best of my abilities.
Here's a quick sketch of how the CPU processes instructions:
Instruction code <- Instruction Pointer <- Memory Data <- Instruction code (Data Element, opt.) Data <- Registers Data <- Data pointer <- MemoryEach instruction code can perform operations on data from the registers, main memory, or it's own 1-4 byte data elements. The actual instruction code can be from 1 - 17 bytes long, the only required byte(s) being the opcode which tells the processor what operation to perform. The other bytes of the instruction code specify modifiers for the operation and data elements (0-4 bytes) for the operation. Data registers are essentially memory locations on the processor itself. This makes them the fastest way to store bits of information, but the space is very limited (x86 defines eight 32bit registers). Assembly language adds a layer of abstraction to this by adding mnemonics for the opcodes.
55 89 E5 83 EC 08Translates to:
push %ebp mov %esp, %ebp sub $0x8, %espIt also lets you declare and name your data.
testvalue: .long 150 message: .ascii "This is a test message." pi: .float 3.14159In summary, assembly language may look extremely cryptic, but it is at least human-readable and it gives the programmer access to the core of a CPU's processing. So here's my first assembly language program:
# cpuid.s Sample program to extract the processor Vendor ID .section .data output: .ascii "The processor Vendor ID is 'xxxxxxxxxxxx'\n" .section .bss .section .text .globl _start _start: movl $0, %eax cpuid movl $output, %edi movl %ebx, 28(%edi) movl %edx, 32(%edi) movl %ecx, 36(%edi) movl $4, %eax movl $1, %ebx movl $output, %ecx movl $42, %edx int $0x80 movl $1, %eax movl $0, %ebx int $0x80Which I assembled and linked with the following commands:
$ as -o cpuid.o cpuid.s $ ld -o cpuid cpuid.oAnd the output:
$ ./cpuid The processor Vendor ID is 'AuthenticAMD'Hooray!
No comments:
Post a Comment