COMP 2003: Assembly Language and Digital Logic

Slides:



Advertisements
Similar presentations
Register In computer architecture, a processor register is a small amount of storage available on the CPU whose contents can be accessed more quickly than.
Advertisements

Chapter 2 (cont.) An Introduction to the 80x86 Microprocessor Family Objectives: The different addressing modes and instruction types available The usefulness.
Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
ENGS 116 Lecture 41 Instruction Set Design Part II Introduction to Pipelining Vincent H. Berk September 28, 2005 Reading for today: Chapter 2.1 – 2.12,
Chapter 3 Addressing Modes
Lect 3: Instruction Set and Addressing Modes. 386 Instruction Set (3.4) –Basic Instruction Set : 8086/8088 instruction set –Extended Instruction Set :
Azir ALIU 1 What is an assembly language?. Azir ALIU 2 Inside the CPU.
Stack Memory H H FFFFF H FFFFE H SS 0105 SP 0008 TOS BOS BOS = FFFF = 1104F H H 1104F H.
The 8086 Assembly Programming Data Allocation & Addressing Modes
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Assembly Language Advantages 1. It reveals the secret of your computer’s hardware and software. 2. Speed. 3. Some special applications and occasions. Disadvantages.
ICS312 Set 3 Pentium Registers. Intel 8086 Family of Microprocessors All of the Intel chips from the 8086 to the latest pentium, have similar architectures.
Microprocessor Systems Design I
Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.
Microprocessor Systems Design I Instructor: Dr. Michael Geiger Spring 2014 Lecture 4: x86 memory.
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 2 The Microprocessor and its Architecture.
Lect 4: Instruction Set and Addressing Modes. 386 Instruction Set (3.4)  Basic Instruction Set : 8086/8088 instruction set  Extended Instruction Set.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
CDP ECE Spring 2000 ECE 291 Spring 2000 Lecture 7: More on Addressing Modes, Structures, and Stack Constantine D. Polychronopoulos Professor, ECE.
CSC 221 Computer Organization and Assembly Language
Low Level Programming Lecturer: Duncan Smeed Overview of IA-32 Part 1.
COMP 2003: Assembly Language and Digital Logic Chapter 0: Some Background Notes by Neil Dickson.
INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
CET 3510 Microcomputer Systems Tech. Lecture 2 Professor: Dr. José M. Reyes Álamo.
Arithmetic Flags and Instructions
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
(-133)*33+44* *33+44*14 Input device memory calculator Output device controller Control bus data bus memory.
Chapter 2 Parts of a Computer System. 2.1 PC Hardware: Memory.
Carnegie Mellon 1 Machine-Level Programming I: Basics Lecture, Feb. 21, 2013 These slides are from website which accompanies the.
Computers organization & Assembly Language Chapter 1 THE 80x86 MICROPROCESSOR.
MOV Instruction MOV destination,source  MOV AX,BX  MOV SUM,EAX  MOV EDX,ARRAY[EBX][ESI]  MOV CL,5  MOV DL,[BX]
Computer Architecture and Assembly Language
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
Assembly Language Data Movement Instructions. MOV Instruction Move source operand to destination mov destination, source The source and destination are.
Microprocessor, Programming & Interfacing Tutorial 2- Module 3.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Intel MP Organization. Registers - storage locations found inside the processor for temporary storage of data 1- Data Registers (16-bit) AX, BX, CX, DX.
Addressing Modes Instruction – Op-code – Operand Addressing mode indicates a way of locating data or operands. – Any instruction may belong to one or more.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Computer Science 516 Intel x86 Overview. Intel x86 Family Eight-bit 8080, 8085 – 1970s 16-bit 8086 – was internally 16 bits, externally 8 bits.
Assembly language programming
Instruction Set Architecture
Homework Reading Lab with your assigned section starts next week
Credits and Disclaimers
Assembly Lab 3.
Microprocessor Systems Design I
Microprocessor and Assembly Language
Chapter 4 Data Movement Instructions
Assembly IA-32.
Assembly Language Programming Part 2
Homework Reading Continue work on mp1
Assembly Language Programming V: In-line Assembly Code
BIC 10503: COMPUTER ARCHITECTURE
Data Addressing Modes • MOV AX,BX; This instruction transfers the word contents of the source-register(BX) into the destination register(AX). • The source.
8086 Registers Module M14.2 Sections 9.2, 10.1.
32-bit instruction mode(80386-Pentium 4 only)
CS 301 Fall 2002 Computer Organization
Chapter 3: Addressing Modes
The Microprocessor & Its Architecture
CNET 315 Microprocessor & Assembly Language
Computer Architecture CST 250
EECE.3170 Microprocessor Systems Design I
X86 Assembly Review.
Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Instructor: John Barr * Modified slides from the book.
CSC 497/583 Advanced Topics in Computer Security
Chapter 8: Instruction Set 8086 CPU Architecture
Credits and Disclaimers
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

COMP 2003: Assembly Language and Digital Logic Chapter 6: Becoming the Machine Notes by Neil Dickson This chapter discusses machine code: what all executables are made of. Note that the examples go into great detail, but don’t be frightened, since knowing the gory details is much less important than understanding generally how instructions are encoded. The examples are just to give a flavour of the variations that can occur.

Machine Code The CPU doesn’t understand text Need a concise way of representing an instruction such that it is easy (fast) for the CPU to determine what to do This representation is called machine code

Example of Machine Code address encoding source code 000001F4 F7 E3 mul ebx 000001F6 BB 00000000 mov ebx,0 000001FB NextPixel: 000001FB 3B D8 cmp ebx,eax 000001FD 73 0C jae Done 000001FF C7 04 99 mov dword ptr [ecx+ebx*4],00080FFh 000080FF 00000206 83 C3 01 add ebx,1 00000209 EB F0 jmp NextPixel 0000020B Done: 0000020B C3 ret 0000020C Notice that the line labels take up no space. They are just names for addresses.

Example of Machine Code address encoding source code 000001F4 F7 E3 mul ebx 000001F6 BB 00000000 mov ebx,0 000001FB NextPixel: 000001FB 3B D8 cmp ebx,eax 000001FD 73 0C jae Done 000001FF C7 04 99 mov dword ptr [ecx+ebx*4],00080FFh 000080FF 00000206 83 C3 01 add ebx,1 00000209 EB F0 jmp NextPixel 0000020B Done: 0000020B C3 ret 0000020C +2 +5 +2 +2 +7 +3 +2 +1 Notice that the increase in address is the size of the instruction.

x86 Instruction Machine Code prefix(es) REX prefix opcode mod-reg-r/m SIB offset immediate opcode: main indication of what the instruction is; looked up in an opcode map the only part present in all instructions may be multiple bytes, or use reg in mod-reg-r/m if only one operand or an immediate mod-reg-r/m byte: mod (high 2 bits): 0 = r/m is memory & no offset; 1 = memory & 8-bit offset; 2 = memory & 32-bit offset; 3 = r/m is a register reg (middle 3 bits): specifies the register (eax=0 to edi=7) used as the register operand r/m (low 3 bits): if mod=3, specifies the other register used as an operand, else specifies an addressing register scale-index-base byte: allows 2 addressing registers; present iff mod≠3 and r/m=4 (esp) scale (high 2 bits): power of two by which to multiply the index register (0reg; 1reg*2; 2reg*4; 3reg*4) index (middle 3 bits): addressing register to be multiplied by 2scale base (low 3 bits): addressing register not to be multiplied only esp used for addressing if index=4 (esp) and base=4 (esp) prefixes: most common prefix is 66h, which changes the operand size from dwords to words

Let’s look back at our example code Register Numbers eax ax al 1 ecx 1 cx 1 cl 2 edx 2 dx 2 dl 3 ebx 3 bx 3 bl 4 esp 4 sp 4 ah 5 ebp 5 bp 5 ch 6 esi 6 si 6 dh 7 edi 7 di 7 bh Let’s look back at our example code

Decoding Machine Code 11011000  mod=11=3=both registers; reg=011=3=ebx; r/m=000=0=eax address encoding source code 000001F4 F7 E3 mul ebx 000001F6 BB 00000000 mov ebx,0 000001FB NextPixel: 000001FB 3B D8 cmp ebx,eax 000001FD 73 0C jae Done 000001FF C7 04 99 mov dword ptr [ecx+ebx*4],00080FFh 000080FF 00000206 83 C3 01 add ebx,1 00000209 EB F0 jmp NextPixel 0000020B Done: 0000020B C3 ret 0000020C opcode map says that: 3B  cmp register,dword ptr register/memory & followed by mod-reg-r/m

Decoding Machine Code address encoding source code 000001F4 F7 E3 mul ebx 000001F6 BB 00000000 mov ebx,0 000001FB NextPixel: 000001FB 3B D8 cmp ebx,eax 000001FD 73 0C jae Done 000001FF C7 04 99 mov dword ptr [ecx+ebx*4],00080FFh 000080FF 00000206 83 C3 01 add ebx,1 00000209 EB F0 jmp NextPixel 0000020B Done: 0000020B C3 ret 0000020C opcode map says that BB  mov ebx,constant & followed by 32-bit constant opcode map says that C3  ret

Decoding Machine Code 00000100  mod=0=no offset; reg is ignored; r/m=4=followed by SIB address encoding source code 000001F4 F7 E3 mul ebx 000001F6 BB 00000000 mov ebx,0 000001FB NextPixel: 000001FB 3B D8 cmp ebx,eax 000001FD 73 0C jae Done 000001FF C7 04 99 mov dword ptr [ecx+ebx*4],00080FFh 000080FF 00000206 83 C3 01 add ebx,1 00000209 EB F0 jmp NextPixel 0000020B Done: 0000020B C3 ret 0000020C 10011001  scale=2=index*4; index=3=ebx; base=1=ecx opcode map says that C7  mov dword ptr register/memory,constant & followed by mod-reg-r/m & 32-bit constant at the end

Decoding Machine Code 11100011  mod=3=register; reg=4=mul in opcode map; r/m=3=ebx address encoding source code 000001F4 F7 E3 mul ebx 000001F6 BB 00000000 mov ebx,0 000001FB NextPixel: 000001FB 3B D8 cmp ebx,eax 000001FD 73 0C jae Done 000001FF C7 04 99 mov dword ptr [ecx+ebx*4],00080FFh 000080FF 00000206 83 C3 01 add ebx,1 00000209 EB F0 jmp NextPixel 0000020B Done: 0000020B C3 ret 0000020C opcode map says that F7  ??? dword ptr register/memory & followed by mod-reg-r/m where reg specifies the operation (from not, neg, mul, div, ...) similar for the add instruction

What about jumps and calls? Opcode indicates that it is a jump or call and the condition (if conditional jump) Opcode is followed by a signed constant that is the number to add to eip if the condition is met i.e. jumps and calls are relative to the following instruction because eip contains the address of the following instruction

Decoding Machine Code Jumps 000001FF (address of following instruction) + 0C = 0000020B, address of Done address encoding source code 000001F4 F7 E3 mul ebx 000001F6 BB 00000000 mov ebx,0 000001FB NextPixel: 000001FB 3B D8 cmp ebx,eax 000001FD 73 0C jae Done 000001FF C7 04 99 mov dword ptr [ecx+ebx*4],00080FFh 000080FF 00000206 83 C3 01 add ebx,1 00000209 EB F0 jmp NextPixel 0000020B Done: 0000020B C3 ret 0000020C opcode map says that 73  jae LineLabel & followed by 8-bit signed relative address of LineLabel

Decoding Machine Code Jumps 0000020B (address of following instruction) + FFFFFFF0 = 0000020B + (-10) = 000001FB, address of NextPixel sign-extended address encoding source code 000001F4 F7 E3 mul ebx 000001F6 BB 00000000 mov ebx,0 000001FB NextPixel: 000001FB 3B D8 cmp ebx,eax 000001FD 73 0C jae Done 000001FF C7 04 99 mov dword ptr [ecx+ebx*4],00080FFh 000080FF 00000206 83 C3 01 add ebx,1 00000209 EB F0 jmp NextPixel 0000020B Done: 0000020B C3 ret 0000020C Note: Jumps beyond -128 bytes or +127 bytes and all calls have a 32-bit relative address instead. opcode map says that EB  jmp LineLabel & followed by 8-bit signed relative address of LineLabel