Anda di halaman 1dari 43

Basic for Assembly Programming

Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003

Assembly Language
Assembly language: Assembly language is used for most programming because it is extremely difficult to program a microprocessor in its native, that is hexadecimal machine language. Assembler: An assembler is a program that converts software written in symbolic machine language (the source programme) into hexadecimal machine language (object programme). The primary reason to use assembler is because development and modification are always difficult in machine language.

Registers in 80x86 microprocessor


General Purpose Registers AX Accumulator BX Base register CX Counting register DX Data register
15 AX BX CX DX 8 7 AH 0 AL Accumulator Base Counter Data

BH
CH DH

BL
CL DL

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Registers in 80x86 microprocessor


Segment Registers CS Code Segment register DS Data Segment register SS Stack Segment register ES Extra Segment register

CS = F000H IP = 1234H Physical Address = F0000 H + 1234 H = F1234 H


Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003

Registers in 80x86 microprocessor

Special Register BP Base Pointers register SI Source Index register DI Destiny Index register SP Stack pointer register IP Next Instruction Pointer register F Flag register O, D, I, T, S, Z, A, P, C

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Flag Register
OF DF IF TF SF ZF AF PF CF

11 10

OF DF IF TF SF ZF AF PF CF

Over Flow Direction Interrupt Trap Sign Zero Auxiliary Parity Carry

Over flow from arithmetic Inc or Dec for moving or compare string Enable or Disable all external interrupt Enable or Disable single step operation Sign bit for arithmetic number, 1 = negative Result is zero Carry out for bit 3 on 8-bit data Even or Odd parity of low-order, 8-bit data Carry out from MSB, Left most of arithmetic

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Addressing Mode
Register
Immediate
MOV CX,DX
MOV AL,22h

Direct

MOV CX,[1234h]

Register Indirect MOV AX,[BX] ; SI,DI,BP,BX

Base Plus Index MOV CX,[BX+ DI] ; BP, BX + SI, DI


Register Relative
MOV AX, [BX+1000h]

Base Relative Plus Index


Computer Organization and Assembly Language #2

MOV AX,[BX+SI+100h] MOV AX,ARRAY[BX+SI]


C. Vongchumyen 12 / 2003

Assembly Template
Page 60,132 Title Exasm1 skeleton of an assembly Program ; -----------------------------------------------------------STACKSG Segment Para stack 'Stack' STACKSG Ends DATASG DATASG CODESG Begin Assume mov mov mov int Begin CODESG Segment Para 'Data' Ends Segment Para 'Code' Proc far CS:CODESG, DS:DATASG, SS:STACKSG ax,DATASG ds,ax ... ax,4C00h 21h endp Ends Begin

End

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Assembly Template
Page 60,132 60 Lines per page 132 Character per line on listing file ( .LST )

Title Exasm1 skeleton of an assembly Program This text will show on every page of listing file

; ------------------------------------------Comment, Start with ; ( Semi colon )

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Assembly Template
STACKSG Segment Para Defining Stack Segment . STACKSG Ends Name
Alignment : Para Combine : Stack Class Type : Stack

stack 'Stack

Segment Align Combine class


= start address in slot of 10H = Combine segment with other when linked = Group related segment when linking

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Assembly Template
DATASG Segment Defining Data Segment . DATASG Ends
CODESG Segment Defining Code Segment . CODESG Ends

Para 'Data

Para 'Code

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Assembly Template
Begin Assume mov mov Begin ... endp Proc far CS:CODESG, DS:DATASG, SS:STACKSG ax,DATASG ds,ax

Defining Procedure Begin Proc Begin endp Defining Segment Assume

Far

CS:CODESG, DS:DATASG, SS:STACKSG

Set Address of Data Segment mov ax,DATASG mov ds,ax

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Assembly Template
Set Program Entry Point End Begin

Exit Code mov int

ax,4C00h 21h

Using Dos Interrupt Function 4CH Return 00H as exit code to DOS

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Assembly Template ( next one )


.Model TINY .STACK .DATA

.CODE
ORG 100H mov mov mov int end
Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003

ax,@data ds,ax ax,4C00h 21h

Assembly Template ( next one )


. Model Mode Code Data Stack
Max Total

TINY
Small
Compact

64 KB
64 KB 64 KB
64 KB Each 64 KB Each 64 KB Each

64 KB
64 KB 128 KB 640 KB 640 KB 640 KB 640 KB

64 KB Each 64 KB 64 KB Each 64 KB Each

Medium Large Huge

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Assembly Template ( next one )


.STACK Defining Stack Area
.DATA Defining Data Area .CODE Defining Code Area

End End Program Directive

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Assembly Template ( next one )

Set Address of Data segment Mov ax,@data Mov ds,ax ORG 100H First Op-Code locate at Offset 100H ( Required for .COM File )

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Assemble and Linking


Step of Assembly Programming Create Source Code Assemble Link Test and Debug
Tool For Assembly Programming 1. Text Editor 2. Assembler ( TASM, MASM ) 3. Linker ( TLINK, LINK ) 4. Debugger ( Debug, Turbo Debugger)

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Assemble and Linking


C:\TASM\BIN>TASM test.asm ; /l = generate listing file Turbo Assembler Version 4.1 Copyright (c) 1988, 1996 Borland International
Assembling file: test.asm Error messages: None Warning messages: None Passes: 1 Remaining memory: 454k

.OBJ

.EXE
C:\TASM\BIN>TLINK test Turbo Link Version 5.1 Copyright (c) 1992 Borland International

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Frequency Error & Warning


.Model TINY .STACK .DATA

.CODE
ORG 100H mov mov mov int ax,@data ds,ax ax,4C00h 21h

**Fatal** test.asm(xx) Unexpected end of file encountered


Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003

Frequency Error & Warning


.Model TINY
.CODE ORG 100H mov mov mov int end

ax,@data ds,ax
ax,4C00h 21h Warning: No stack

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Basic Instruction

Instruction : MOV Purpose : Data transfer between memory cells, registers and the accumulator. Syntax : MOV Destiny, Source

Example : MOV AX,0006h MOV BX,AX MOV AX,4C00h

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Basic Instruction

Instruction : LEA Purpose : Syntax : To load the address of the source operator LEA destiny, source

Example : LEA SI,VAR1 equivalent to: MOV SI,OFFSET VAR1

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Basic Instruction

Instruction : LAHF Purpose : It transfers the content of the flags to the AH register Syntax : LAHF

Example :

LAHF

This instruction is useful to verify the state of the flags during the execution of our program

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Basic Instruction

Instruction : POP Purpose : It recovers a piece of information from the stack Syntax : POP destiny

Example :

POP AX

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Basic Instruction

Instruction : POPF Purpose : Syntax : It extracts the flags stored on the stack POPF

Example :

POPF

BIT FLAG 0 CF, 2 PF, 4 AF, 6 ZF, 7 SF, 8 TF, 9 IF, 10 DF, 11 OF

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Basic Instruction

Instruction : PUSH Purpose : Syntax : Example : It places a word on the stack PUSH source PUSH AX

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Basic Instruction
Instruction : PUSHF
Purpose : It places the value of the flags on the stack.

Syntax :
Example :

PUSHF
PUSHF

This command decreases by 2 the value of the SP register and then the content of the flag register is transferred to the stack, on the address indicated by SP. The flags are left stored in memory on the same bits indicated on the POPF command.
Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003

Basic Instruction
Instruction : AND
Purpose : It performs the conjunction of the operators bit by bit. Syntax : AND destiny, source

Example : AND AX,0FF00H AND AX,BX AND AX,[BX]

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Basic Instruction
Instruction : NEG
Purpose : It generates the complement to 2.

Syntax :
Example :

NEG destiny
NEG AX

For example, if AX stores the value of 1234H, then: NEG AX This would leave the 0EDCCH value stored on the AX register.

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Basic Instruction

Instruction : NOT Purpose : It carries out the negation of the destiny operator bit by bit. NOT destiny

Syntax :

Example :

NOT AX

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Basic Instruction
Instruction : OR
Purpose : Syntax : Example : OR OR Logic inclusive OR OR destiny, source OR AX,0FF00H AX,BX AX,[BX]

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Basic Instruction
Instruction : XOR
Purpose : Syntax : Logic exclusive OR XOR destiny, source

Example : XOR AX,0FF00H XOR AX,BX XOR AX,[BX]

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Basic Instruction
Instruction : TEST
Purpose : Syntax : It logically compares the operators TEST destiny, source

Example : TEST AX,0FF00H TEST AX,BX TEST AX,[BX] It performs a conjunction, bit by bit, of the operators, but differing from AND, this instruction does not place the result on the destiny operator, it only has effect on the state of the flags.
Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003

Basic Instruction

Instruction : ADC Purpose : Syntax : addition with carry ADC destiny, source

Example : ADC AX,0FF00H ADC AX,BX ADC AX,[BX]

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Basic Instruction

Instruction : ADD Purpose : Syntax : addition without carry ADD destiny, source

Example : ADD AX,0FF00H ADD AX,BX ADD AX,[BX]

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Basic Instruction

Instruction : DIV Purpose : Syntax : Example : Division without sign. DIV source DIV BL ( 8 Bit ) AL = AX / BL AH = Remainder BX ( 16 Bit ) AX = DX:AX / BX DX = Remainder
C. Vongchumyen 12 / 2003

DIV

Computer Organization and Assembly Language #2

Basic Instruction
Instruction : IDIV Purpose : Syntax : Division with sign. IDIV source

Example : IDIV BL ( 8 Bit ) IDIV BX ( 16 Bit ) basically consists on the same as the DIV instruction, and the only difference is that this one performs the operation with sign. For its results it used the same registers as the DIV instruction.
Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003

Basic Instruction

Instruction : MUL Purpose : Syntax : Example : Multiplication without sign. MUL source

MUL CH ( 8 Bit ) AX = CH * AL MUL BX ( 16 Bit ) DX:AX = BX * AX

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Basic Instruction

Instruction : IMUL Purpose : Syntax : Example : Integer Multiplication with sign. IMUL source

IMUL CH ( 8 Bit ) AX = CH * AL IMUL BX ( 16 Bit ) DX:AX = BX * AX

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Basic Instruction

Instruction : SUB Purpose : Syntax : Example : SUB SUB subtraction without carry SUB destiny, source SUB AX,0FF00H AX,BX AX,[BX]

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Basic Instruction

Instruction : SBB Purpose : Syntax : Example : SBB SBB subtraction without borrow SBB destiny, source SBB AX,0FF00H AX,BX AX,[BX]

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003

Basic Instruction

Instruction : SBB Purpose : Syntax : Example : SBB SBB subtraction without borrow SBB destiny, source SBB AX,0FF00H AX,BX AX,[BX]

Computer Organization and Assembly Language #2

C. Vongchumyen 12 / 2003