Blogging about our lives online.


Assembly Language - Instruction Codes

Source Text: Professional Assembly Language (Blum, 2005) Chapter 1:
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 <- Memory 
Each 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.
89 E5
83 EC 08
Translates to:
 push %ebp
mov %esp, %ebp
sub $0x8, %esp
It also lets you declare and name your data.
 .long 150
 .ascii "This is a test message."
 .float 3.14159
In 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
.ascii "The processor Vendor ID is 'xxxxxxxxxxxx'\n"

.section .bss

.section .text
.globl _start

movl $0, %eax
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 $0x80
Which I assembled and linked with the following commands:
$ as -o cpuid.o cpuid.s
$ ld -o cpuid cpuid.o
And the output:
$ ./cpuid
The processor Vendor ID is 'AuthenticAMD'

No comments:

Post a Comment