introduction to assembly language (with new lessons)

106
8/14/2019 Introduction to Assembly Language (with new lessons) http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 1/106 Assembly Programming by Introduction to Introduction to Assembly Language Assembly Language 2 nd Semester SY 2009-2010 Benjie A. Pabroa

Upload: daryl-ivan-hisola

Post on 30-May-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 1/106

Assembly Programming by

Introduction toIntroduction to

Assembly LanguageAssembly Language2nd Semester SY 2009-2010

Benjie A. Pabroa

Page 2: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 2/106

Assembly Programming by

"High"-level languages such as BASIC,FORTRAN, Pascal, Lisp, APL, etc. aredesigned to ease the strain of programming by providing the user with a

set of somewhat sophisticated operationsthat are easily accessed

What is AssemblyWhat is AssemblyLanguageLanguage

Page 3: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 3/106

Assembly Programming by

 The lesson we derive is this: a very low-levellanguage might be very flexible andefficient (in terms of speed and memoryuse), but might be very difficult to program

in since no sophisticated operations areprovided and since the programmer mustunderstand in detail the operation of thecomputer

Assembly language is essentially the lowestpossible level of language.

Assembly as Low-levelAssembly as Low-levellanguagelanguage

Page 4: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 4/106

Assembly Programming by

the ability to read the values stored atvarious "memory locations",

the ability to write a new value into amemory location,

the ability to do integer arithmetic of limitedprecision (add, subtract, multiply, divide),

 The ability to do logical operations (or, and,not, xor),

and the ability to " jump" to programs storedat various locations in the computer'smemory.

Built-in FeaturesBuilt-in Features

Page 5: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 5/106

Assembly Programming by

 The ability to perform graphics and the ability to access files ability to directly perform floating-point

arithmeti

Features not includedFeatures not included

Page 6: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 6/106

Assembly Programming by

FORTRAN code to average together the N numbers storedin the array X(I):

INTEGER*2 I,X(N) INTEGER*4 AVG . . .

AVERAGE THE ARRAY X, STORING THE RESULT AS AVG:

 AVG=0 DO 10 I=1,N

 AVG=AVG+X(I)  AVG=AVG/N . . .

Assembly vs High LevelAssembly vs High LevelLangLang

Page 7: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 7/106Assembly Programming by

 mov cx,n ; cx is used as the loop ; counter. It starts at N and

; counts down to zero.

 mov dx,0 ; the dx register stores the

; two most significant bytes of

; the running sum mov ax,0 ; use ax to store the least

; significant bytes

 mov si,offset x ; use the si register to point

; to the currently accessed

; element X(I), starting with

; I=0

Assembly vs High LevelAssembly vs High LevelLangLang

Page 8: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 8/106Assembly Programming by

addloop:add ax,word ptr [si] ; add X(I) to the two least

; significant bytes of AVG

adc dx,0 ; add the "carry" into the two

; most significant bytes of AVG

add si,2 ; move si to point to X(I+1)loop addloop ; decrement cx and loop again

; if not zero

div n ; divides AVG by N

 mov avg,ax ; save the result as AVG

Assembly vs High LevelAssembly vs High LevelLangLang

Page 9: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 9/106Assembly Programming by

writing it required intimate knowledge of how the variables x, n, and avg werestored in memory.

Assembly vs High LevelAssembly vs High LevelLangLang

Page 10: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 10/106Assembly Programming by

Microprocessor◦ Reading instructions from the memory and

executing them Access memory

Do arithmetic and logical operationsPerforms other services as well

PC System ArchitecturePC System Architecture

Page 11: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 11/106Assembly Programming by

1971:◦ Intel’s 4004 was the first microprocessor—a 4-bit CPU (like the one

from CS231) that fit all on one chip. 1978:

◦  The 8086 was one of the earliest 16-bit processors.

1981:◦ IBM uses the 8088 in their little PC project.

1989:◦  The 80486 includes a floating-point unit in the same chip as the main

processor, and uses RISC-based implementation ideas like pipeliningfor greatly increased performance.

1997:◦  The Pentium II is superscalar, supports multiprocessing, and includes

special instructions for multimedia applications. 2002:

◦  The Pentium 4 runs at insane clock rates (3.06 GHz), implementsextended multimedia instructions and has a large on-chip cache.

PC System ArchitecturePC System Architecture

Page 12: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 12/106Assembly Programming by

Memory◦ Store instructions(program) or data

◦ It appears as a sequence of locations(oraddresses)

Each address – stored a byte◦  Types:ROM Stored byte may only be read by the CPU

Cannot be changed

RAMStored byte may be both read and

written(changed)

Volatile – all data will be lost after shutdown

Both types are random access

PC System Architecture..PC System Architecture..

Page 13: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 13/106Assembly Programming by

Assembly language is a compiled language◦ Source-code must first be created with a text-

editor program

◦  Then the source-code will be compiled

◦ Assembly language compilers => assemblers Auxiliary Programs

◦ First: text-editor(source code editor)

◦ Second: assemblerAssembles source code to generate object code

in the process.◦  Third: LinkerCombines object code modules created by

assembler

The Process of AssemblyThe Process of Assembly

Page 14: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 14/106Assembly Programming by

◦ Fourth: LoaderBuilt-in to the operating system and is never

explicitly executed.

 Takes the “relocatable” code created by thelinker, “loads: it into memory at the lowest

available location, then runs it.

◦ Fifth: DebuggerEnvironment for running and testing assembly

language programs.

The Process of Assembly..The Process of Assembly..

Page 15: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 15/106Assembly Programming by

The Process of Assembly..The Process of Assembly..

Source Code

     A    s     s     e     m

     b      l    e 

     r

Object Code Linker 

Other Object Code1Other Object Code2

Relocatable Code

RAM

Loader 

Page 16: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 16/106Assembly Programming by

DOS◦ provides the environment in which programs

run.

◦ Provides a set of helpful utility functions

Must be understood in order to create programin DOS

DOS and Simple FileDOS and Simple FileOperationOperation

Page 17: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 17/106Assembly Programming by

 You can use the edit command in DOS or just use the notepad.

Making an assembly SourceMaking an assembly SourceCodeCode

Page 18: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 18/106

Assembly Programming by

AH AL

BH BL

CH CL

DH DL

SP

BP

SI

DI

CS

DS

SS

ES

Bus Control Unit

1

2

3

4

Instruction Pointer 

ALU

CU

Flag Register

Page 19: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 19/106

Assembly Programming by

Assembly language◦  Thought goes into the use of the computer

memory and the CPU registers

Register◦ Like a memory location in that it can store a

byte (or work) value.

◦ No address in the memory, it is not part of thecomputer memory(built into the CPU)

CPU RegistersCPU Registers

Page 20: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 20/106

Assembly Programming by

Importance of Registers in Assembly Prog.◦ Instructions using registers > operating on

values stored at memory locations.

◦ Instructions tend to be shorter (less room to

store in memory)◦ Register-oriented instructions operate faster that

memory-oriented instructionsSince the computer hardware can access a

register much faster than a memory location.

CPU RegistersCPU Registers

Page 21: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 21/106

Page 22: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 22/106

Assembly Programming by

Segment RegistersSegment Registers

CS Code Segment 16-bit number that points tothe active code-segment

DS Data Segment 16-bit number that points tothe active data-segment

SS Stack Segment 16-bit number that points tothe active stack-segment

ES Extra Segment 16-bit number that points tothe active extra-segment

Page 23: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 23/106

Assembly Programming by

IP Instruction Pointer 16-bit number that points to the offset of the next instruction

SP Stack Pointer 16-bit number that points to the offsetthat the stack is using

BP Base Pointer used to pass data toand from the stack 

Pointer RegistersPointer Registers

Page 24: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 24/106

Assembly Programming by

AX Accumulator Register mostly used for  calculations and for input/output

BX Base Register Only register that can

 be used as an index

CX Count Register register used for theloop instruction

DX Data Register input/output and used by multiply anddivide

General Purpose RegistersGeneral Purpose Registers

Page 25: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 25/106

Assembly Programming by

SI Source Index used by stringoperations assource

DI Destination Index used by stringoperations asdestination

Index RegistersIndex Registers

Page 26: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 26/106

Assembly Programming by

◦ AX, BX, CX, & DX – more flexible that otherCan be used as word registers(16-bit val)

Or as a pairs of byte registers (8-bit vals)

◦ A General purpose registers can be “split”AX = AH + AL

BX = BH + BL

CX = CH + CL

DX = DH + DL

◦ Ex: DX = 1234h, then DH = 12h and DL = 34h

CPU registersCPU registers

Page 27: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 27/106

Assembly Programming by

Consist of 9 status bits(flags) Flags – because it can be either

◦ SET(1)

◦ NOT SET(0)

Flag RegistersFlag Registers

Page 28: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 28/106

Assembly Programming by

Abr. Name bit nº Description

OF Overflow Flag 11 indicates an overflow when set 

DF Direction Flag 10 used for string operations tocheck direction

IF Interrupt Flag 9 if   set , interrupt are enabled,else disabled

TF Trap Flag 8 if   set , CPU can work insingle step mode

SF Sign Flag 7 if   set , resulting number of calculation is negative

Flag RegistersFlag Registers

Page 29: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 29/106

Assembly Programming by

Abr. Name bit nº DescriptionZF Zero Flag 6 if   set , resulting number 

of calculation is zero

AF Auxiliary Carry 4 some sort of secondcarry flag

PF Parity Flag 2 indicates even or odd parity

CF Carry Flag 0 contains the left-most bitafter calculations

Flag Registers..Flag Registers..

Page 30: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 30/106

Assembly Programming by

 You want to see all these register and flags?◦ go to DOS

◦  Type debug

◦ type "r"

◦  The you’ll see all the registers and someabbreviations for the flags.

◦  Type "q" to quit again.

Test itTest it

Page 31: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 31/106

Assembly Programming by

How DOS uses memory◦ databus = 16-bitit can move and store 16 bits(1 word = 2 bytes)

at a time.

If the processor store 1 word (16-bits) it storesthe bytes in reverse order in the memory.1234h (word) ---> memory 34h (byte) 12h

(byte) Memory value: 78h 56h

derived value 5678h

Memory SegmentationMemory Segmentation

Page 32: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 32/106

Assembly Programming by

Computer divides it memory into segments◦ Standard in DOS

◦ Segments are 64KB big and have a number

◦  These numbers are stored in the segment

registers (see above).◦  Three main segments are the code, data and

stack segment Overlap each other almost completely

 Try type d in the debug4576:0100 -> memory address

where 4576 – segment number; 0100 – offset

Memory Segmentation..Memory Segmentation..

Page 33: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 33/106

Assembly Programming by

Segments overlaps◦  The address 0000:0010 = 0001:0000

◦  Therefore, segments starts at paragraphboundaries

A paragraph = 16 bytesSo a segment starts at an address divisible by 16

◦ 0000:0010 => 0h:10h => 0:16Memory Location: (0*16)+16 = 0+16 = 16 (linear

address)

◦ 0001:0000 => 1h:0h => 1:0Memory Location: (1*16)+0 = 16+0 = 16 (linear

address)

Memory Segmentation..Memory Segmentation..

.model small

Page 34: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 34/106

Assembly Programming by B

My First Program.stack.data message db "Hello world, I'm learning Assembly !!!", "$"

.code

 main proc  mov ax,seg message  mov ds,ax

 mov ah,09 lea dx,message int 21h

 mov ax,4c00h int 21h main endpend main

Page 35: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 35/106

Assembly Programming by

Identifiers◦ An identifier is a name you apply to items in

your program. the two types of identifiers are"name", which refers to the address of a dataitem, and "label", which refers to the address

of an instruction. The same rules apply tonames and labels

Statements◦

A program is made of a set of statements, thereare two types of statements, "instructions"such as MOV and LEA, and "directives" whichtell the assembler to perform a specific action,like ".model small“ or “.code”

NamesNames

Page 36: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 36/106

Page 37: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 37/106

Page 38: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 38/106

Assembly Programming by

 The source code can only be assembled byan assembler or and the linker.◦ A86

◦ MASM

◦  TASM – we will use this one Install TASM Then use the tasm.exe and tlink.exe

How to AssembleHow to Assemble

Page 39: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 39/106

Assembly Programming by B

How to Assemble

•  The Assemble

 – To assemble Type the ff. on thecommand prompt:

• cd c:\tasm\bin• tasm <filename/path of the source code>

 – tasm c:\first.asm

• tlink <filename/path of the object code>

 – tlink c:\tasm\bin\first.obj or  – tlink first.obj

 – To run call the .exe on the commandprompt:

• Example in our program(First.asm)

Page 40: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 40/106

Page 41: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 41/106

Assembly Programming by

.model small ◦ Lines that start with a "." are used to provide the assembler

with information.

◦  The word(s) behind it say what kind of info. In this case it just tells the assembler that the program is small

and doesn't need a lot of memory. I'll get back on this later.

.stack  ◦  This one tells the assembler that the "stack" segment starts

here. The stack is used to store temporary data.

.data ◦ indicates that the data segment starts here and that the stack

segment ends there. 

Dissecting CodeDissecting Code

d l ll

Page 42: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 42/106

Assembly Programming by

.model small

.stack

.data

 message db "Hello world, I'm learning Assembly !!!", "$"

.code

 main proc mov ax,seg message

 mov ds,ax

 mov ah,09

lea dx,message

int 21h

 mov ax,4c00h

int 21h

 main endp

end main

Page 43: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 43/106

Assembly Programming by

.code ◦ indicates that the code segment starts there and the data

segment ends there.

main proc ◦ Code must be in procedures, just like in C or any other language.◦  This indicates a procedure called main starts here.◦ endp states that the procedure is finished.◦ endmain main : tells the assembler that the program is finished.

◦ It also tells the assembler where to start in the program. At the procedure called main in this case.

message db "xxxx" ◦ DB means Define Byte and so it does.◦ In the data-segment it defines a couple of bytes.

◦  These bytes contain the information between the brackets.◦ "Message" is a name to indentify this byte-string.◦ It's called an "indentifier". 

Dissecting Code..Dissecting Code..

Page 44: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 44/106

Assembly Programming by

Memory space for variables◦ DB (Byte – 8 bit )

◦ DW (Word – 16 bit)

◦ DD (Doubleword – 32 bit)◦ Example:

foo db 27 ;by default all numbers are decimal

 bar dw 3e1h ; appending an "h" means hexadecimal

real_fat_rat dd ? ; "?" means "don't care about the value“

◦ Variable name Address can’t be changed

Value can be changed

Page 45: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 45/106

Page 46: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 46/106

Page 47: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 47/106

Assembly Programming by

Syntax:

◦ MOV destination , source

Allows you to move data into and out the

registers◦ Destinationeither registers or mem. Loc.

◦ Sourcecan be either registers, mem. Loc. or numeric

value

Memory-to-memory transfer NOT ALLOWED

The MOV InstructionThe MOV Instruction

Page 48: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 48/106

Assembly Programming by

foo db 27 ;by default all numbers are decimal

 bar dw 3e1h ; appending an "h" means hexadecimal real_fat_rat dd ? ; "?" means "don't care about the value“

 mov ax,bar ; load the word-size register ax with

; the word value stored at location bar.  mov dl,foo ; load the byte-size register dl with

; the byte value stored at location foo.

 mov bx,ax ; load the word-size register bx with

; the byte value in ax.

 mov bl,ch ; load the byte-size register bl with

; the byte value in ch.

 mov bar,si ; store the value in the word-size

; register si at the memory location ; labelled "bar".

 mov foo,dh ; store the byte value in the register

; dh at memory location foo.

 mov ax,5 ; store the word 5 in the ax register.

 mov al,5 ; store the byte 5 in the al register.

 mov bar,5 ; store the word 5 at location bar.

 mov foo,5 ; store the byte 5 at location foo.

The MOV InstructionThe MOV Instruction

Codes we do earlier 

tice the size of the source and destination

(must match inreg-reg,

mem-reg,reg-memTransfers)

nstant must consistent with the destination

Page 49: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 49/106

Assembly Programming by

 MOV AL, 3172◦ MOV foo, 3172

Why the code above are Illegal?

Illegal Move StatementIllegal Move Statement

model small

Page 50: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 50/106

Assembly Programming by

.model small

.stack

.data

 message db "Hello world, I'm learning Assembly !!!", "$"

.code

 main proc

 mov ax, seg message

 mov ds,ax

 mov ah,09

lea dx,messageint 21h

 mov ax,4c00h

int 21h

 main endp

end main

Page 51: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 51/106

Assembly Programming by

mov ds,ax ◦ Here it moves the number in the AX register (the number of 

the data segment) into the DS register.◦ We have to load this DS register this way (with two

instructions)◦  Just typing: "mov ds,segment message" isn't possible.

mov ah, 09 ◦ MOV again. This time it load the AH register with the constant

value nine.

lea dx, message ◦ LEA - Load Effective Address.  This instructions stores the offset within the datasegment of the

bit-string message into the DX register. This offset is the second thing we need to know, when we want to

know where "message" is in the memory.So now we have DS:DX.

Dissecting Code..Dissecting Code..

Page 52: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 52/106

model small

Page 53: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 53/106

Assembly Programming by

.model small

.stack

.data

 message db "Hello world, I'm learning Assembly !!!", "$"

.code

 main proc

 mov ax,seg message

 mov ds,ax

 mov ah,09

lea dx,message

int 21h

 mov ax,4c00hint 21h

 main endp

end main

Page 54: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 54/106

Assembly Programming by

int 21h◦  This instruction causes an Interrupt.◦  The processor calls a routine somewhere in memory.◦ 21h tells the processor what kind of routine, in this case a DOS

routine.◦ For now assume that INT just calls a procedure from DOS.◦  The procedure looks at the AH register to find out what it has to do.◦ In this example the value 9 in the AH register indicates that the

procedure should write a bit-string to the screen.

mov ax, 4c00h◦ Load the Ax register with the constant value 4c00h

int 21h◦ this time the AH register contains the value 4ch (AX=4c00h) and to

the DOS procedure that means "exit program".◦  The value of AL is used as an "exit-code" 00h means "No error"

Dissecting Code..Dissecting Code..

Page 55: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 55/106

Page 56: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 56/106

Assembly Programming by

0F77:0000 B8C813 MOV AX,13C8

0F77:0003 8ED8 MOV DS,AX

0F77:0005 B409 MOV AH,09

Segment Number & Offset

Machine Code instruction

0F77:0000 B8790F MOV AX,0F79 

originally: mov ax, seg messageB8 ->mov ax790F ->number 

It means that data is store in the segment with number 0F79

Th th i t ti l d

Page 57: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 57/106

Assembly Programming by

 The other instruction lea dx,message turned into mov dx,0.◦ So that means that the offset of the bit-string is

0 --> 13C8:0000.◦  Try to type d 13C8:0000

◦ Calculating other address

We will subtract 2 segments from 13C8 = 13C62 segments = 32 bit (0002:0000)

 The other address is 13C6:0020

Page 58: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 58/106

Page 59: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 59/106

Assembly Programming by

The stack is a place where data istemporarily stored

 The SS and SP registers point to that placelike this: SS:SP◦ So the SS register is the segment and the SP

register contains the offset

 There are a few instructions that make useof the stack

◦ PUSH - Push a value on the stack◦ POP - retrieve that value from the stack

The Stack The Stack 

Page 60: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 60/106

Page 61: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 61/106

Assembly Programming by

 MOV AX, 1234H MOV BX, 5678H

PUSH AX

POP BX

◦ We pushed the AX to the stack

◦ and we popped that value in BX.◦

◦ What is the final value of AX and BX?

The Stack The Stack 

Page 62: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 62/106

Assembly Programming by

It is easy done by the instruction .stack thatwill create a stack of 1024 bytes.

 The stack uses a LIFO system (Last In FirstOut)

The Stack The Stack 

Page 63: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 63/106

Page 64: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 64/106

Page 65: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 65/106

Assembly Programming by

If you fully understand this stuff (registers,flags, segments, stack, names, etc.) youmay, from now on, call yourself a

"Level 0 Assembly Coder"

Congatulation!!Congatulation!!

A Simple Sample ProgramA Simple Sample Program

Page 66: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 66/106

Assembly Programming by

Suppose that we have 4 word-sized valuesstored in the variables MY, NAME, IS, NOBODY,(initial values 4, 5, 6, and 32) and that wewant to move these values to the variablesPLAY, MISTY, FOR, ME.

Fortran Prog INTEGER*2 MY,NAME,IS,NOBODY,PLAY,MISTY,FOR,ME DATA MY,NAME,IS,NOBODY/4,5,6,32/

....

PLAY=MY

 MISTY=NAME

FOR=IS ME=NOBODY

....

A Simple Sample ProgramA Simple Sample ProgramFragmentFragment

Page 67: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 67/106

Page 68: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 68/106

Assembly Programming by

We can write program in DEBUG◦ The reason for this is that with DEBUG we can

concentrate our thoughts purely on assemblylanguage

DEBUG◦ System Debugger

◦ Has its own built-in editor and primitiveassembler

◦ Its code does not need to be linked

◦ also has facilities for modifying memorylocations and for examining memorylocations

DEBUGDEBUG

Page 69: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 69/106

Page 70: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 70/106

Assembly Programming by

“My Name is Nobody” program debugversion◦ Let say we the byte variables MY, NAME, IS, NOBODY,

PLAY, MISTY, FOR, and ME to reside at memory locations

200 (hex) through 207.

Our program might looks like this:

 mov ax,[200] ; PLAY=MY

 mov [204],ax mov ax,[201] ;  MISTY=NAME

 mov [205],ax mov ax,[202] ; FOR=IS

 mov [206],ax mov ax,[203] ;  ME=NOBODY

 mov [207],ax

DEBUG..DEBUG..

Page 71: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 71/106

Assembly Programming by

 The program may be entered with the "A" or "assemble“ command followed by the address. (Annnn)-a10048EE:0100 mov ax,[200]48EE:0103 mov [204],ax48EE:0106 mov ax,[201]48EE:0109 mov [205],ax48EE:010C mov ax,[202]48EE:010F mov [206],ax48EE:0112 mov ax,[203]48EE:0115 mov [207],ax

48EE:0118

Entering a blank line terminates this process

DEBUG..DEBUG..

Page 72: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 72/106

Assembly Programming by

DEBUGDEBUG

Page 73: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 73/106

Assembly Programming by

We can check that the program is actually in the computer

at address 100 with the "U" or "unassemble"command.

DEBUG..DEBUG..

You may also type in U100,118 to specify the ending line to

DEBUG Program

Page 74: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 74/106

Assembly Programming by

DEBUG Program

MOV AX,[200] assembler 

RAM

A10002

U command“Unassembler”

Deduced CodeMOV AX,[200]

Page 75: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 75/106

Assembly Programming by

Executable Program Loader 

RAM

A10002

Page 76: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 76/106

Assembly Programming by

initialize the variables MY, NAME, IS, and NOBODY (which is to say,

the values stored at memory locations 200 through 203).

◦ can be done with the "E" or "enter" instruction (Ennnn) E200

419F:0200 77.4 20.5 64.6 69.20

where 77,20,64,69 are the original values of stored at the address <space> moves cursor to the next address <enter> terminated enter command

◦ Can be also possible to use DB and DW using A: -a200

419F:200 db 4419F:201 db 5419F:202 db 6419F:203 db 20

DEBUG..DEBUG..

Page 77: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 77/106

DEBUG

Page 78: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 78/106

Assembly Programming by

View entered values using d or “display” command◦ dnnn – display from address nnnn

◦ dnnn,mmmm - display from nnnn to mmmm

DEBUG..DEBUG..

DEBUG

Page 79: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 79/106

Assembly Programming by

Running the program◦ Using G or “Go” command G=nnnn,mmmm – runs the program from address nnnn to mmmm

In our case:

G=100,118

◦ Verify if it really works by displaying the location 200 to 207-d200,207

419F:0200 04 05 06 20 72 65 63 74 .......◦

DEBUGDEBUG

DEBUGDEBUG

Page 80: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 80/106

Assembly Programming by

DEBUG..DEBUG..

DEBUGDEBUG

Page 81: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 81/106

Assembly Programming by

DEBUG..DEBUG..

DEBUGDEBUG

Page 82: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 82/106

Assembly Programming by

 Terminating Debugger◦ Q or “Quit Command”

DEBUG..DEBUG..

Page 83: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 83/106

DEBUGDEBUG

Page 84: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 84/106

Assembly Programming by

Modifying RegistersRrn – where rn is the name of the

registers(AX,BX...)

Ex. to store 4567 (hex) in the CX register

DEBUG..DEBUG..

A ith ti I t tiA ith ti I t ti

Page 85: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 85/106

Assembly Programming by

Instructions◦ ADD – Additional

◦ SUB - Subtraction

Syntax

◦ mnemonic destination, source◦ ADD destination, source

◦ SUB destination, source

 Things to remember:◦

no memory-to-memory operations are allowed◦ the source operand can be an immediate value

◦ the sizes of the source and destination operands mustmatch

Arithmetic InstructionsArithmetic Instructions

A ith ti I t tiA ith ti I t ti

Page 86: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 86/106

Assembly Programming by

 The ADD (SUB) instruction adds (subtracts) thevalue of the source operand to (from) the valueof the destination operand, and stores theresult in the destination

Arithmetic InstructionsArithmetic Instructions

Page 87: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 87/106

Page 88: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 88/106

A ti itActivity

Page 89: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 89/106

Assembly Programming by

Give the code of MYNAME IS NOBODY

Modify the program

which at this timewe wanted to dothe equivalent of ◦ PLAY=MY+1

◦ MISTY=NAME-1

◦ FOR=IS+1◦ ME=NOBODY-1

ActivityActivityMY NAME IS NOBODY Program

; destination variables

 play db ?

 misty db ?

for db ?

 me db ?

; source variables my db 4

name db 5

is db 6

nobody db 32

.....

 mov al,my mov play,al

 mov al,name

 mov misty,al

 mov al,is

 mov for,al

 mov al,nobody

Page 90: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 90/106

Assembly Programming by

Page 91: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 91/106

Assembly Programming by

Page 92: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 92/106

INC and DECINC and DEC

Page 93: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 93/106

Assembly Programming by

Smaller and executes more quickly INC

◦ INC destination

◦ Adds one to destination

DEC◦ DEC destination

◦ Subtracts one from destination

INC and DECINC and DEC

Page 94: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 94/106

Assembly Programming by

Carry FlagCarry Flag

Page 95: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 95/106

Assembly Programming by

It can happen in integer addition that theresult of an addition is too big for thedestination address to hold◦ the carry flag is used to store both carries and

borrows in integer addition and subtraction

◦ Ex: MOV AL,200 MOV BL,195 MOV CL,25 ADD AL,BL

the carry flag would be "set" to one, and theresult would be truncated to 8 bits: i.e., ALwould contain 139.

Carry FlagCarry Flag

Page 96: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 96/106

ADC and SBBADC and SBB

Page 97: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 97/106

Assembly Programming by

ADC◦ ADC destination,source

◦ "add with carry“

◦ ADC automatically adds in the carry left overfrom previous operations

SBB◦ SBB destination,source

◦ "subtract with borrow“

◦ SBB automatically subtracts the borrow

ADC and SBBADC and SBB

Page 98: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 98/106

Assembly Programming by

Page 99: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 99/106

 Jumps and Conditional Jumps and Conditional

Page 100: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 100/106

Assembly Programming by

 The control is accomplished with "jump"instructions.◦  Jump instructions have the syntax

mnemonic address

◦  The mnemonic here can be a number of differentthings, but for the moment, we will assume thatit is "JMP".

◦ A JMP instruction "jumps“ from the present locationin memory (as indicated by the instructionpointer register IP) to the specified address in

memory. In essence, JMP simply stores the givenaddress in the IP register.

J p Jumps.. Jumps..

 Jumps and Conditional Jumps and Conditional

Page 101: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 101/106

Assembly Programming by

In DEBUG, the address operand is, of course,simply a number. For example, if we executed the instruction

◦  JMP 121

then the very next instruction executedwould be the instruction located at address121h.

p Jumps.. Jumps..

Page 102: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 102/106

 Jumps and Conditional Jumps and Conditional

Page 103: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 103/106

Assembly Programming by

 JMP performs an unconditional jump:◦ it always goes to the specified address,

regardless of any special conditions that mayobtain.

 There are also a series of conditional jumpinstructions which perform a jump only if some special condition is met.◦ These instructions all have the general syntax

given above, but their

 Jumps.. Jumps..

 Jumps and Conditional Jumps and Conditional

Page 104: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 104/106

Assembly Programming by

 Jumps.. Jumps..

 Jumps and Conditional Jumps and Conditional

Page 105: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 105/106

Assembly Programming by

 Jumps.. Jumps..

 Jumps and Conditional Jumps and Conditional

Page 106: Introduction to Assembly Language (with new lessons)

8/14/2019 Introduction to Assembly Language (with new lessons)

http://slidepdf.com/reader/full/introduction-to-assembly-language-with-new-lessons 106/106

 Jumps.. Jumps..