assembly language

23
Assembly Language Assembly Language CS 6303 CS 6303 Rashad Sierra Rashad Sierra

Upload: kyrie

Post on 23-Jan-2016

40 views

Category:

Documents


0 download

DESCRIPTION

Assembly Language. CS 6303 Rashad Sierra. What is Assembly Language and why do we need it?. The CPU only understands machine language. Machine language is “a binary language, which means that all machine language instructions are in binary form” (Runnion pg2) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Assembly Language

Assembly LanguageAssembly Language

CS 6303CS 6303

Rashad SierraRashad Sierra

Page 2: Assembly Language

What is Assembly Language and What is Assembly Language and why do we need it?why do we need it?

The CPU only understands machine language. The CPU only understands machine language.

Machine language is “a binary language, which Machine language is “a binary language, which means that all machine language instructions are means that all machine language instructions are in binary form” (Runnion pg2)in binary form” (Runnion pg2)

““Assembly Language is just a symbolic form of Assembly Language is just a symbolic form of machine language.” (Runnion pg2)machine language.” (Runnion pg2)

In order to run on the CPU, every program must In order to run on the CPU, every program must be converted into machine language.be converted into machine language.

In essence we are programming within the CPU.In essence we are programming within the CPU.

Page 3: Assembly Language

The CPU DesignThe CPU Design

Graphic taken from Art of Assembly by Randall HydeGraphic taken from Art of Assembly by Randall Hyde

Page 4: Assembly Language

AssemblerAssembler

The Assembler performs the translation The Assembler performs the translation from assembly language to machine from assembly language to machine language.language.It is essentially a compiler that lets you It is essentially a compiler that lets you address and access memory directly. address and access memory directly. It has a defined Instruction set consisting It has a defined Instruction set consisting of opcodes and operands.of opcodes and operands.It gives you access to the processor It gives you access to the processor registers set.registers set.

Page 5: Assembly Language

The Registers SetThe Registers Set

““The Register set consist of all registers in the The Register set consist of all registers in the CPU that are accessible to the programmer.” CPU that are accessible to the programmer.” (Mano & Kime, pg 484)(Mano & Kime, pg 484) A register is the fastest of all temporary storage A register is the fastest of all temporary storage type because it is a logical circuit that only type because it is a logical circuit that only depends on the clock and inputdepends on the clock and inputAll the registers are 16 bits long in Intel 8088 All the registers are 16 bits long in Intel 8088 processor. processor. There are eight general-purpose registers, four There are eight general-purpose registers, four Segment Registers, and two Control Registers Segment Registers, and two Control Registers available to the programmer.available to the programmer.

Page 6: Assembly Language

General Purpose RegistersGeneral Purpose Registers

The General Registers have three The General Registers have three categories categories

A.A. Data Registers (AX,BX,CX,DX)Data Registers (AX,BX,CX,DX)

B.B. Pointer Registers (BP,SP)Pointer Registers (BP,SP)

C.C. Index Registers (DI,SI)Index Registers (DI,SI)

Page 7: Assembly Language

Data RegistersData RegistersAX (Accumulator register)- used for arithmetic AX (Accumulator register)- used for arithmetic operations.operations.

BX (Base register)- used to access an item in BX (Base register)- used to access an item in data structure. Ex. Element in arraydata structure. Ex. Element in array

CX (Count register) - used as a counter in loop CX (Count register) - used as a counter in loop operationsoperations

DX (Data Register) – used as an extension of DX (Data Register) – used as an extension of AX for 32 bit arithmeticAX for 32 bit arithmetic

Each 16 bit register can be separated into two 8 Each 16 bit register can be separated into two 8 bit registers bit registers AH-High order 8 Bits AH-High order 8 Bits AL-Low order 8 BitsAL-Low order 8 Bits

Page 8: Assembly Language

Pointer RegistersPointer Registers

SP (Stack Pointer)- Its used together with SP (Stack Pointer)- Its used together with the Stack Segment to calculate the top of the Stack Segment to calculate the top of the stackthe stack SS:SPSS:SP

BP (Base Pointer)- useful as an auxiliary BP (Base Pointer)- useful as an auxiliary stack segment pointer.stack segment pointer.

Page 9: Assembly Language

Index RegistersIndex Registers

DI (Destination Index Registers) - used DI (Destination Index Registers) - used with with string instructions.string instructions.

SI (source Index Register) – it two is used SI (source Index Register) – it two is used for string instructions and manipulationsfor string instructions and manipulations

Page 10: Assembly Language

Segment RegistersSegment RegistersCS (Code Segment)- contains the origin address CS (Code Segment)- contains the origin address of the active code segment. Contains of the active code segment. Contains Executable instructions.Executable instructions.

SS (Stack Segment)- contains the origin address SS (Stack Segment)- contains the origin address of the active stack segment. Contains dynamic of the active stack segment. Contains dynamic data that is pushed or popped on the stack.data that is pushed or popped on the stack.

DS (Data Segment)-contains the origin address DS (Data Segment)-contains the origin address of the active stack segment. Contains static dataof the active stack segment. Contains static data

ES (Extra Segment)- contains the origin address ES (Extra Segment)- contains the origin address of the active extra segment. Extra segment for of the active extra segment. Extra segment for data segment.data segment.

Page 11: Assembly Language

Control RegistersControl RegistersIP (Instruction Pointer) – The offset in the code IP (Instruction Pointer) – The offset in the code segment where the next instruction is. CS:IPsegment where the next instruction is. CS:IPFlag Register – a 16 bit register that indicates Flag Register – a 16 bit register that indicates the current state of the microprocessor.the current state of the microprocessor. Overflow Flag- allows overflow or notOverflow Flag- allows overflow or not Direction Flag- to add or subtract from index registersDirection Flag- to add or subtract from index registers Interrupt Flag- allows system interruptsInterrupt Flag- allows system interrupts Trap Flag- allows to trace single-step executionTrap Flag- allows to trace single-step execution Sign Flag- signed or unsigned numbersSign Flag- signed or unsigned numbers Zero Flag- tests if operation produces zeroZero Flag- tests if operation produces zero Auxiliary Carry Flag- carry or borrow in arithmetic Auxiliary Carry Flag- carry or borrow in arithmetic

operationsoperations Parity Flag- parity bit is odd or evenParity Flag- parity bit is odd or even Carry Flag- used to detect overflowCarry Flag- used to detect overflow

Page 12: Assembly Language

Flag RegisterFlag RegisterGraphic taken from “A guide to Debug” Graphic taken from “A guide to Debug”

http://mirror.href.com/thestarman/asm/debug/8086REGs.htm#REGShttp://mirror.href.com/thestarman/asm/debug/8086REGs.htm#REGS

Page 13: Assembly Language

Instruction categoriesInstruction categories

Data Transfer InstructionsData Transfer Instructions

Stack InstructionsStack Instructions

Data Manipulation InstructionsData Manipulation Instructions ArithmeticArithmetic Logical Logical

Program Control InstructionsProgram Control Instructions

Interrupt InstructionsInterrupt Instructions

Page 14: Assembly Language

Data Transfer InstructionsData Transfer Instructions

MOVE INSTRUCTIONMOVE INSTRUCTION

MOV <destination>,<source>MOV <destination>,<source> MOV AX,0001MOV AX,0001

EXCHAGE INSTRUCTIONEXCHAGE INSTRUCTION

XCHG <destination>,<source>XCHG <destination>,<source> XCHG AX,DXXCHG AX,DX

Page 15: Assembly Language

Stack InstructionsStack InstructionsWe cannot directly move or exchange data to We cannot directly move or exchange data to the stack because it is not a register. The stack the stack because it is not a register. The stack is a block of contiguous memory addresses.is a block of contiguous memory addresses.

We must push the data on to the stack and pop We must push the data on to the stack and pop it off the stack.it off the stack.

PUSH <source>PUSH <source>

POP <destination>POP <destination>

We can push the 16 bit Flag register on to the We can push the 16 bit Flag register on to the stack by using the followingstack by using the following

PUSHFPUSHF

POPFPOPF

Page 16: Assembly Language

Data Manipulation InstructionsData Manipulation InstructionsArithmetic InstructionsArithmetic Instructions

ADDITIONADDITIONADD <Destination>,<source>ADD <Destination>,<source>

ADD AX,0001ADD AX,0001

SUBTRACTIONSUBTRACTIONSUB <Destination>,<source>SUB <Destination>,<source>

SUB AX,0001SUB AX,0001

MULTIPLICATIONMULTIPLICATIONMUL <source>MUL <source>

MUL BL ; Multiply AL=AL*BL notice only low bitsMUL BL ; Multiply AL=AL*BL notice only low bitsIMUL <source>IMUL <source>

IMUL BX ; Integer Multiply AX=AX*BX notice the whole 16 bits IMUL BX ; Integer Multiply AX=AX*BX notice the whole 16 bits

DIVISIONDIVISIONDIV <source>DIV <source>

DIV BL ; Divide AL=AL/BL notice only low bits.DIV BL ; Divide AL=AL/BL notice only low bits.IDIV <source>IDIV <source>

IDIV BX ; Integer Divide AX=AX/BX notice the whole 16 bits.IDIV BX ; Integer Divide AX=AX/BX notice the whole 16 bits.

Page 17: Assembly Language

Data Manipulation Instructions #2Data Manipulation Instructions #2Logical InstructionLogical Instruction

Logical ANDLogical AND AND <destination>,<source>AND <destination>,<source>

AND AL,BLAND AL,BL

Logical ORLogical OROR <destination>,<source>OR <destination>,<source>

OR AL,BLOR AL,BL

Exclusive ORExclusive ORXOR <destination>,<source>XOR <destination>,<source>

XOR AL,BLXOR AL,BL

NEGATE/ 2’S COMPLEMENTNEGATE/ 2’S COMPLEMENTNEG <destination>NEG <destination>

NET ALNET AL

NOT/ 1’S COMPLEMENTNOT/ 1’S COMPLEMENTNOT <destination>NOT <destination>

NOT ALNOT AL

Page 18: Assembly Language

Program Control InstructionsProgram Control InstructionsJMP <destination> ; Unconditional JumpJMP <destination> ; Unconditional Jump

CMP <address1>,<address2> ; Compare and CMP <address1>,<address2> ; Compare and set flagsset flags

JA <destination> ; Jump if aboveJA <destination> ; Jump if above

JB <destination>; Jump if bellowJB <destination>; Jump if bellow

JE <destination>; Jump if EqualJE <destination>; Jump if Equal

JNE <destination>; Jump if not equalJNE <destination>; Jump if not equal

Instructions are used to make logical decisions Instructions are used to make logical decisions in a programin a program

Page 19: Assembly Language

Interrupt InstructionsInterrupt InstructionsAn interrupt is “an external request for service-a An interrupt is “an external request for service-a request for the processor to stop executing a request for the processor to stop executing a procedure and to being executing a procedure procedure and to being executing a procedure that has been previously designated to service that has been previously designated to service interrupts of the designated type” (Runnion, pg interrupts of the designated type” (Runnion, pg 374)374)

We will use Interrupts to get Input/Output (I/O) We will use Interrupts to get Input/Output (I/O) in the assembly language.in the assembly language.

We can use interrupts to write onto a hard disk, We can use interrupts to write onto a hard disk, read data from the keyboard, and display read data from the keyboard, and display information to the monitor.information to the monitor.

Page 20: Assembly Language

Printing ! to the ScreenPrinting ! to the ScreenMOV AH,02 ; Interrupt instruction to printMOV AH,02 ; Interrupt instruction to print

MOV DL,24 ; 24 ASCII code for ‘!’MOV DL,24 ; 24 ASCII code for ‘!’

INT 21 ;INT 21 ; Call the Interrupt 21Call the Interrupt 21

MOV AH,4C; Interrupt instruction to returnMOV AH,4C; Interrupt instruction to return

INT 21; Call INT 21INT 21; Call INT 21

Page 21: Assembly Language

Reading Char from keyboard until ‘!’Reading Char from keyboard until ‘!’Assume instruction start at memory address 100Assume instruction start at memory address 100

JMP 108 ; Jump to memory locationJMP 108 ; Jump to memory location

MOV AH,02 ; Move 02 Print instructionMOV AH,02 ; Move 02 Print instruction

MOV DL,07 ; Move 07 into DL to beepMOV DL,07 ; Move 07 into DL to beep

INT 21INT 21 ; Call Interrupt 21 ; Call Interrupt 21

MOV AH,01 ; Move 01 Read Char from keyboardMOV AH,01 ; Move 01 Read Char from keyboard

INT 21 ; Call Interrupt 21INT 21 ; Call Interrupt 21

CMP AL,21 ; Compare if Char is ‘!’CMP AL,21 ; Compare if Char is ‘!’

JNZ 102 ; if not print char and read againJNZ 102 ; if not print char and read again

MOV AH,4C ; Move 4C to quit safelyMOV AH,4C ; Move 4C to quit safely

INT 21 ; Call InterruptINT 21 ; Call Interrupt

NOPNOP ; NO Operation ; NO Operation

Page 22: Assembly Language

Debug –Assembly code in actionDebug –Assembly code in actionA small tool called “debug”, which is A small tool called “debug”, which is included in every windows computer, will included in every windows computer, will show the registers and the assembly show the registers and the assembly language instructions.language instructions.

Page 23: Assembly Language

Works CitedWorks CitedRunnion, William C. “Structured Programming Runnion, William C. “Structured Programming in Assembly language for the IBM PC” in Assembly language for the IBM PC”

PWS-KENT –Massachusetts 1998PWS-KENT –Massachusetts 1998

Mano & Kime, M.Morris & Charles. “Logic and Mano & Kime, M.Morris & Charles. “Logic and Computer Design Fundamentals”Computer Design Fundamentals”

Prentice Hall – New Jersey 2004Prentice Hall – New Jersey 2004

Hyde, Randall. “The Art of Assembly” Hyde, Randall. “The Art of Assembly” No Starch Press- California 2003No Starch Press- California 2003