assembly operationsweb.cse.ohio-state.edu/~reeves.92/cse2421au12/slidesday...assembly the cpu uses...

11
Assembly The CPU uses machine language to perform all its operations Machine code (pure numbers) is generated by translating each instruction into binary numbers that the CPU uses This process is called "assembling"; conversely, we can take assembled code and disassemble it into (mostly) human readable assembly language Assembly is a much more readable translation of machine language, and it is what we work with if we need to see what the computer is doing There are many different kinds of assembly languages; we'll focus on the Y86/IA32 language as defined in the text and on our system 283

Upload: others

Post on 15-Mar-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Assembly Operationsweb.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay...Assembly The CPU uses machine language to perform all its operations Machine code (pure numbers) is generated

Assembly

The CPU uses machine language to perform all its operations Machine code (pure numbers) is generated by translating each instruction into binary numbers that the CPU uses This process is called "assembling"; conversely, we can take assembled code and disassemble it into (mostly) human readable assembly language Assembly is a much more readable translation of machine language, and it is what we work with if we need to see what the computer is doing There are many different kinds of assembly languages; we'll focus on the Y86/IA32 language as defined in the text and on our system

283

Page 2: Assembly Operationsweb.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay...Assembly The CPU uses machine language to perform all its operations Machine code (pure numbers) is generated

Assembly Operations

Perform arithmetic function on register or memory data Transfer data between memory and register

Load data from memory into register (read) Store register data into memory (write)

Transfer control Unconditional jumps to/from procedures (calls) Conditional branches (if, switch, for, while, etc)

284

Page 3: Assembly Operationsweb.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay...Assembly The CPU uses machine language to perform all its operations Machine code (pure numbers) is generated

Machine-level code

ISA – instruction set architecture Format and behavior of a machine level program Defines: The processor state (see the CPU fetch-execute cycle) The format of the instructions The effect of each of these instructions on the state Abstractions Instruction executed “in sequence”

Technically defined to be completing one instruction before starting the next

Pipelining Concurrent execution (but not really) Jumps and calls

Memory addresses are virtual addresses Very large byte-addressable array Address space managed by the OS (virtual physical) Contains both executable code of the program AND its data

» Run-time stack » Block of memory for user (global and heap)

285

Page 4: Assembly Operationsweb.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay...Assembly The CPU uses machine language to perform all its operations Machine code (pure numbers) is generated

CPU Fetch-Execute

cycle

286

An instruction cycle is the basic operation cycle of a computer. It is the process by which a computer retrieves a program instruction from its memory, determines what actions the instruction requires, and carries out those actions. This cycle is repeated continuously by the central processing unit (CPU), from bootup to when the computer is shut down. 1. Fetching the instruction 2. Decode the instruction 3. Memory and addressing issues 4. Execute the instruction

Page 5: Assembly Operationsweb.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay...Assembly The CPU uses machine language to perform all its operations Machine code (pure numbers) is generated

CPU Fetch-Execute cycle

Initiating the cycle The cycle starts immediately when power is applied to the system using an initial PC value that is predefined for the system architecture (in Intel IA-32 CPUs, for instance, the predefined PC value is 0xfffffff0). Typically this address points to instructions in a read-only memory (ROM) which begin the process of loading the operating system. (That loading process is called booting.)

Fetch cycle Step 1 of the Instruction Cycle is called the Fetch Cycle. These steps are the same for each instruction. The fetch cycle processes the instruction from the instruction word which contains an opcode.

Decode Step 2 of the instruction Cycle is called the decode. The opcode fetched from the memory is being decoded for the next steps and moved to the appropriate registers.

Read the effective address Step 3 is deciding which operation it is. If this is a Memory operation - in this step the computer checks if it's a direct or indirect memory operation: Direct memory instruction - Nothing is being done. Indirect memory instruction - The effective address is being read from the memory. If this is a I/O or Register instruction - the computer checks its kind and execute the instruction.

Execute cycle Step 4 of the Instruction Cycle is the Execute Cycle. These steps will change with each instruction.

287

Page 6: Assembly Operationsweb.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay...Assembly The CPU uses machine language to perform all its operations Machine code (pure numbers) is generated

Machine code vs C code

Program Counter (PC) Register %eip Address in memory of the next instruction to be executed

Integer Register File Contains eight named locations for storing 32-bit values Can hold addresses (C pointers) or integer data Have other special duties

Condition Code registers Hold status information About arithmetic or logical instruction executed

CF (carry flag) OF (overflow flag) SF (sign flag) ZF (zero flag)

Floating point registers

288

Page 7: Assembly Operationsweb.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay...Assembly The CPU uses machine language to perform all its operations Machine code (pure numbers) is generated

Machine Instruction Example

C code Add two signed integers

Assembly Add 2 4-byte integers

Operands X: register %eax Y: memory M[%ebp+8] T: register %eax Return function value in %eax

Object code 3 byte instruction Stored at address: 0x????????

289

int t = x + y; addl 8(%ebp),%eax 03 45 08

Page 8: Assembly Operationsweb.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay...Assembly The CPU uses machine language to perform all its operations Machine code (pure numbers) is generated

IA32

32-bit address bus normal physical address space of 4 GBytes (232

bytes) addresses ranging continuously from 0 to 0xFFFFFFFF

Complex instruction set (CISC) machine No pipelining (only with RISC machines) Data formats

Primitive data types of C Single byte suffix denotes size of operand No aggregate types Arrays, structures

Registers (see handout)

six (almost) general purpose 32-bit registers: %eax, %ebx, %ecx, %edx, %esi, %edi two specialty stack pointer and base/frame pointer: %esp, %ebp Float values are in different registers (later)

290

C Declaration Suffix Name Size

char B BYTE 8 bits

short W WORD 16 bits

int L LONG 32 bits

char * (pointer) L LONG 32 bits

float S SINGLE 32 bits

Page 9: Assembly Operationsweb.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay...Assembly The CPU uses machine language to perform all its operations Machine code (pure numbers) is generated

Operand Specifiers

Source operand Constants, registers, or memory

Destination operand Registers or memory

CANNOT DO MEMORY-MEMORY TRANSFER WITH A SINGLE INSTRUCTION 3 types

Immediate – for constant values Register Memory

291

Page 10: Assembly Operationsweb.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay...Assembly The CPU uses machine language to perform all its operations Machine code (pure numbers) is generated

Operand Combinations example

Source Dest Src,Dest* C analog

Immediate Register movl $0x4, %eax temp = 0x4;

Immediate Memory movl $-147, (%eax) *p = -147;

Register Register movl %eax, %edx temp2 = temp1;

Register Memory movl %eax, (%edx) *p = temp;

Memory Register movl (%eax), %edx temp = *p;

292

• Each statement should be viewed separately • REMINDER: cannot do memory-memory transfer with a single instruction

Page 11: Assembly Operationsweb.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay...Assembly The CPU uses machine language to perform all its operations Machine code (pure numbers) is generated

Operand addressing example

Address Value Operand Value Comment 0x100 0xFF %eax 0x100 Register 0x104 0xAB 0x104 0xAB Absolute Address 0x108 0x13 $0x108 0x108 Immediate 0x10C 0x11 (%eax) 0xFF Address 0x100 - indirect

4(%eax) 0XAB Address 0x104 - base+displacement Register Value 9(%eax,%edx) 0X11 Address 0x10C - indexed %eax 0x100 260(%ecx,%edx) 0X13 Address 0x108 - indexed %ecx 0x1 0xFC(,%ecx,4) 0XFF Address 0x100 - scaled index* %edx 0x3 (%eax,%edx,4) 0X11 Address 0x10C - scaled index*

293

First two columns in blue are given as is the Operand FYI: 260 decimal = 104 hex *scaled means to multiply the 2nd argument by the scaled value (the 3rd argument)