Sie sind auf Seite 1von 63

Fundamental of Assembly Language

Programming (for Microprocessor)


Prima Dewi Purnamasari
Microprocessor
Electrical Engineering Department
Universitas Indonesia
Computer Language
 High Level language
 Pascal, C, C++, Java, etc
 Low Level Language
 Assembly
 Machine Codes
 010010001010100101010  in binary
 1234 FFAB 1234 H  in hexadecimal

2 Microprocessor (c) Prima Dewi Purnamasari 2011


Why Assembly?
 Assembly has several features that make it a good choice many
some situations.
1. It's fast – Assembly programs are generally faster than
programs created in higher level languages. Often,
programmers write speed-essential functions in assembly.
2. It's powerful –You are given unlimited power over your
assembly programs. Sometimes, higher level languages have
restrictions that make implementing certain things difficult.
3. It's small – Assembly programs are often much smaller
than programs written in other languages. This can be very
useful if space is an issue.

3 Microprocessor (c) Prima Dewi Purnamasari 2011


Preparation for Assembly Programming
 Basically you will need:
 Program editor  as simple as Notepad
 Assembler
1. MASM  http://www.masm32.com/.
2. TASM  Made by Borland, a commercial product
3. NASM  http://sourceforge.net/projects/nasm/

 Be careful in writing your programs, because it runs directly


on your microprocessor!

4 Microprocessor (c) Prima Dewi Purnamasari 2011


Steps to Create a
Program

5 Microprocessor (c) Prima Dewi Purnamasari 2011


Creating an Assembly Language Program
 An assembly language program should be written with any
text editor and have the extension filename.asm.
 The assembler and Linker
 The assembler program converts a symbolic source module
(file) into a hexadecimal object file
 The linker program executes as the second part of ML, reads the
object files, created by the assembler program, and links them into
a single execution file (.EXE)

6 Microprocessor (c) Prima Dewi Purnamasari 2011


7 Microprocessor (c) Prima Dewi Purnamasari 2011
MASM32

8 Microprocessor (c) Prima Dewi Purnamasari 2011


TASM

9 Microprocessor (c) Prima Dewi Purnamasari 2011


Emulator
 an emulator is hardware and/or software that duplicates (or
emulates) the functions of a first computer system in a different
second computer system, so that the behavior of the second
system closely resembles the behavior of the first system.

10 Microprocessor (c) Prima Dewi Purnamasari 2011


Emu8086

11 Microprocessor (c) Prima Dewi Purnamasari 2011


Individual Assignment
 Download and install
 emu8086 (trial)
 http://www.emu8086.com/
 Find corresponding tutorial on how to use it (available on the
Internet!), self study!

12 Microprocessor (c) Prima Dewi Purnamasari 2011


Group assignment
 Each group is responsible to bring at minimum 1 laptop
(with emu8086 installed) to class every session

13 Microprocessor (c) Prima Dewi Purnamasari 2011


Assembly Program Structure

14 Microprocessor (c) Prima Dewi Purnamasari 2011


15 Microprocessor (c) Prima Dewi Purnamasari 2011
LIST File, generated
automatically after
program successfully
assembled

Memory Machine
Address codes

16 Microprocessor (c) Prima Dewi Purnamasari 2011


Writing Structure
 NEXT: MOV AX, [BX] ; comment

1 2 3 4

 1= label, followed by “:”


 2= opcode
 3= operand
 4= comment, preceded with”;”
17 Microprocessor (c) Prima Dewi Purnamasari 2011
Writing Structure
 Each statement in an assembly language program consists
of four parts or fields.
 The leftmost field is called the label.
 used to store a symbolic name for the memory location it
represents
 All labels must begin with a letter or one of the following
special characters: @, $, -, or ?.
 a label may have any length from 1 to 35 characters
 The label appears in a program to identify the name of a
memory location for storing data and for other
purposes.
18 Microprocessor (c) Prima Dewi Purnamasari 2011
 The next field to the right is the opcode field.
 designed to hold the instruction, or opcode
 the MOV part of the move data instruction is an example
of an opcode
 Right of the opcode field is the operand field.
 contains information used by the opcode
 the MOV AL,BL instruction has the opcode MOV and
operands AL and BL
 The comment field, the final field, contains a comment about
the instruction(s).
 comments always begin with a semicolon (;)

19 Microprocessor (c) Prima Dewi Purnamasari 2011


 Try it in emulator!
 Click “View” and look the changes in every menu list:
 registers
 Data
 Screen
 Flags
 etc

20 Microprocessor (c) Prima Dewi Purnamasari 2011


Computer Data Formats

21 Microprocessor (c) Prima Dewi Purnamasari 2011


Computer Data Formats
 ASCII and Unicode Data
 Binary Coded Decimal (BCD)
 Byte-Sized Data
 Word-Sized Data
 Doubleword-Sized Data
 Real Numbers

22 Microprocessor (c) Prima Dewi Purnamasari 2011


ASCII Data
 American Standard Code for Information Interchange
(ASCII) data represent alphanumeric characters in the
memory of a computer system (Table 1.7)
 The standard ASCII code is a 7-bit code with the eighth and
MSB used to hold parity in some systems
 ASCII are most often stored in memory using a special
directive to the assembler program called define byte(s) or
DB

23 Microprocessor (c) Prima Dewi Purnamasari 2011


24 Microprocessor (c) Prima Dewi Purnamasari 2011
BCD Data
 Binary-Coded Decimal (BCD) information is stored in
either packed or unpacked forms
 Packed BCD data are stored as two digits per byte
 Unpacked BCD data are stored as one digit per byte
 The range of a BCD digit extends from 00002 to 10012 or
0-9 decimal
 Table 1.9 shows some decimal numbers converted to both
packed ad unpacked BCD

25 Microprocessor (c) Prima Dewi Purnamasari 2011


26 Microprocessor (c) Prima Dewi Purnamasari 2011
Byte-Sized Data
 Byte-size data are stored as unsigned and signed integers
 Negative signed numbers are stored in the 2’s complement
form
 Whenever a number is 2’s complement, its sign changes from
negative to positive or positive to negative
 See example 1-22, 1-23

27 Microprocessor (c) Prima Dewi Purnamasari 2011


28 Microprocessor (c) Prima Dewi Purnamasari 2011
 Define byte (DB) directive is used to store 8-bit data
in memory

Microprocessor (c) Prima Dewi Purnamasari 2011 29


Word-sized Data
 A word (16-bits) is formed with two bytes of data
 The LSB is always stored in the lowest-numbered memory
location, the MSB in the highest (i.e., little endian format)—
used with Intel family of microprocessor
 An alternate method (i.e., big endian format) is used with the
Motorola family of micro-processors

30 Microprocessor (c) Prima Dewi Purnamasari 2011


Word-sized Data
 Fig 1.11(a) & (b) shows the weight of each bit position in a
word of data

31 Microprocessor (c) Prima Dewi Purnamasari 2011


32 Microprocessor (c) Prima Dewi Purnamasari 2011
 Example 1.25 shows several signed and unsigned word-sized
data stored in memory using the assembler program
 Note that define word(s) directive or DW causes the
assembler to store words in the memory

33 Microprocessor (c) Prima Dewi Purnamasari 2011


34 Microprocessor (c) Prima Dewi Purnamasari 2011
Doubleword-sized Data
 Doubleword-sized data requires four bytes of memory
(32-bit number)
 Doubleword-sized data appear as a product after a
multiplication and also as a dividend before a division
 Fig. 1-12 shows the form used to store doublewords in the
memory and the binary weights of each bit position

35 Microprocessor (c) Prima Dewi Purnamasari 2011


36 Microprocessor (c) Prima Dewi Purnamasari 2011
 To define doubleword-sized data, use assembler
directive define doubleword or DD

Microprocessor (c) Prima Dewi Purnamasari 2011 37


Real Numbers
 A real number (floating-point number) contains two parts:
a mantissa, significant, or fraction and an exponent
 Fig. 1-13 and example 1-27 depicts both the 4-byte (single
precision) and 8-byte (double precision) forms of real
numbers

38 Microprocessor (c) Prima Dewi Purnamasari 2011


39 Microprocessor (c) Prima Dewi Purnamasari 2011
 The exponent is stored as a biased exponent
 an exponent of 23 is represented as a biased exponent of 127+3
or 130 (82H) in the single- precision form or as 1026 (402H) in the
double-precision form

40 Microprocessor (c) Prima Dewi Purnamasari 2011


41 Microprocessor (c) Prima Dewi Purnamasari 2011
Assembler detail

From chapter 4

42 Microprocessor (c) Prima Dewi Purnamasari 2011


Directives
 Indicate how an operand or section of program is to be
processed by the assembler
 Storing Data in a Memory Segment: DB, DW, DD, SEGMENT,
.DATA, ENDS, DUP, ALIGN e.g.: Example 4.12
 THIS refers the data as byte or word

43 Microprocessor (c) Prima Dewi Purnamasari 2011


 Memory is reserved for use in the future by using a question
mark (?) as an operand for a DB, DW, or DD directive.
 when ? is used in place of a numeric or ASCII value, the assembler
sets aside a location and does not initialize it to any specific value
 DUP: creates array with or without initial values
 It is important that word-sized data are placed at word
boundaries and doubleword-sized data are placed at
doubleword boundaries.
 if not, the microprocessor spends additional
time accessing these data types

44 Microprocessor (c) Prima Dewi Purnamasari 2011


4 Microprocessor (c) Prima Dewi Purnamasari 2011
5
EQU, ORG ASSUME
 Equate directive (EQU) equates a numeric, ASCII, or label to
another label.
 equates make a program clearer and simplify debugging
 EX: TEN EQU 10 ….
MOV AL,TEN
 The ORG (origin) statement changes the starting offset
address of the data or code segments.
 At times, the origin of data or the code must be assigned to
an absolute offset address with the ORG statement.
 ASSUME tells the assembler what names have been chosen
for the code, data, extra, and stack segments.
 Used only with full-segment definition
46 Microprocessor (c) Prima Dewi Purnamasari 2011
4 Microprocessor (c) Prima Dewi Purnamasari 2011
7
PROC and ENDP
 Indicate start and end of a procedure (subroutine).
 they force structure because the procedure is clearly defined
 Both the PROC and ENDP directives require a label to indicate
the name of the procedure.
 RET instruction executed the end of the proc.
 USES directive indicates which registers are used by the proc.
 The assembler automatically save and restore them using the
stack instructions.
 EX: PRC1 PROC USES AX BX CX
 Use .LISTALL directive to view all instruction generated by
assembler

48 Microprocessor (c) Prima Dewi Purnamasari 2011


4 Microprocessor (c) Prima Dewi Purnamasari 2011
9
5 Microprocessor (c) Prima Dewi Purnamasari 2011
0
Memory Organization
 The assembler uses two basic formats for developing
software:
 one method uses models; the other uses full-segment
definitions
 Memory models are unique to MASM.
 The models are easier to use for simple tasks.
 The full-segment definitions offer better control over
the assembly language task and are recommended for
complex programs.

51 Microprocessor (c) Prima Dewi Purnamasari 2011


Models
 There are many models available to the MASM assembler, ranging
from tiny to huge.
 .MODEL memsize
 TINY: all software and data fit into 64kb memory segment. Useful for small
programs. assembled as a command (.COM) program
 SMALL: one data segment with one code segment for a total of 128kb of
memory. assembled as an execute (.EXE) program
 Start of segments: .CODE, .DATA, .STACK
 Start of instructions and load segment registers with segment
addresses: .STARTUP
 Exit to DOS: .EXIT
 End of file: END
 MP selection : .386, .486, .586, .686 ..

52 Microprocessor (c) Prima Dewi Purnamasari 2011


5 Microprocessor (c) Prima Dewi Purnamasari 2011
3
Full Segment Definitions
 Full-segment definitions are also used with the Borland and
Microsoft C/C++ environments for procedures developed in
assembly language
 More structured form than the model method
 Use assume directive before the program begins.
 The program loader does not automatically initialize DS and
ES. These registers must be loaded in the program
 STACK_SEG, DAT_SEG, CODE_SEG, END MAIN

54 Microprocessor (c) Prima Dewi Purnamasari 2011


STACK_SEG SEGMENT ‘STACK’
DW 100H DUP(?)
STACK_SEG ENDS

DATA_SEG SEGMENT ‘DATA’


LISTA DB 100 DUP(?)
LISTB DB 100 DUP(?)
DATA_SEG ENDS

COSE_SEG SEGMENT ‘CODE’


ASSUME CS:CODE_SEG, DS:DATA_SEG, SS:STACK_SEG
MAIN PROC FAR
MOV AX, DATA_SEG
MOV ES, AX
MOV DS, AX
CLD
MOV SI, OFFSET LISTA
MOV DI, OFFSET LISTB
MOV CX, 100
REP MOVSB
MAIN ENDP
CODE_SEG ENDS
END MAIN

55 Microprocessor (c) Prima Dewi Purnamasari 2011


Introduction to MOV Instruction

56 Microprocessor (c) Prima Dewi Purnamasari 2011


Data Addressing Modes

opcode

an opcode, or operation code, tells


the microprocessor which operation
to perform

MOV instruction provides a basis for explanation of data-addressing modes


57 Microprocessor (c) Prima Dewi Purnamasari 2011
MOV Instruction
 MOV instruction perform COPY of a value, either from or to
memory or register
 MOV = COPY
 MOV ≠ MOVE

58 Microprocessor (c) Prima Dewi Purnamasari 2011


MOV BX, CX

► The source register’s contents do not change. the destination


register’s contents do change
► The contents of the destination register or destination memory
location change for all instructions except the CMP and TEST
instructions.
► Note that only the rightmost 16 bits of register EBX change. The
MOV BX, CX instruction does not affect the leftmost 16 bits of
register EBX
59 Microprocessor (c) Prima Dewi Purnamasari 2011
Some MOV Variant
 MOV AX,BX
 MOV [BX], AX
 [ ] sign represents memory location
 Destination = memory which has address as in BX
 MOV DATA,AX
 DATA is a name the programmer define in DATA SEGMENT
 destination=memory named DATA
 MOV AX,0123H
 a value 0123H is copied to AX register

There are several more, but the above are the fundamental ones

60 Microprocessor (c) Prima Dewi Purnamasari 2011


Rules in addressing. DO NOT:
 Mix different size of register
 MOV AX, BL
 Perform memory to memory addressing
 MOV [1234H],DATA
 Copy content of one segment register to another
 MOV DS,ES
 Use CS as the destination register
 MOV CS,1000H

61 Microprocessor (c) Prima Dewi Purnamasari 2011


Group Assignment—Due Next Session
 Make a program (altogether in one program, sequentially)
 Reserve place for data in data segment namely DATA1 with type
word
 Copy 1234 to AX
 Copy 0011B to AL
 Copy 12H to AH
 Copy AX to DATA1

62 Microprocessor (c) Prima Dewi Purnamasari 2011


The report
 Write the program in emulator
 Compile it.
 Run the program in emulator. (single step). Analyze the effect
on the registers and memory for each line of code
 Written report should be made as comprehensive as it can
be (greater score for better report)

 The main part of your report would be:


1. Print of program (source code), provide sufficient comment
2. Print of LISTING file
3. Program analysis

63 Microprocessor (c) Prima Dewi Purnamasari 2011

Das könnte Ihnen auch gefallen