Sie sind auf Seite 1von 9

Introduction to Assembly Language

An assembly language is a low-level programming language for computers, microprocessors (μP),


microcontrollers (μC), DSP and other programmable devices in which each statement corresponds to
a single machine language instruction. An assembly language is thus specific to a certain physical (or
virtual) computer architecture, in contrast to most high-level programming languages, which, ideally,
are portable. Assembly language is a low-level language because only very simple instructions exist.

Assembly language allows the use of symbolic representation for machine operation codes (usually
called mnemonics), memory locations, registers and other parts of an instruction.
A utility program called an assembler is used to translate assembly language statements into the
target computer's machine code.

Machine languages consist entirely of numbers and are almost impossible for humans to read and
write. Assembly languages have the same structure and set of commands as machine languages, but
they enable a programmer to use names instead of numbers.
Each type of CPU has its own machine language and assembly language, so an assembly language
program written for one type of CPU won't run on another. In the early days of programming, all
programs were written in assembly language. Now, most programs are written in a high-level
language such as JAVA or C. Programmers still use assembly language when speed is essential or
when they need to perform an operation that isn't possible in a high-level language.

Advantages
Makes you better programmer

Performance of the languages like java and C/C++ can be improve as the compilers are written in
assembly and some part of JVM

1
ASSEMBLER
When we write in Assembly language we use abbreviations called mnemonics for certain operations
and functions. This code is also known as source code. The μP however cannot execute and
understand mnemonics. We need to convert the source code or assembly code into machine language
or object code.
A program that translates programs from assembly language to machine language is called
assembler.
Mnemonics/ ASM code machine codes (which pc can understand and know then what to do)
Many assembler are available like nasm , masm , masm32 etc .
For this lab we will use masm 6.15 with windows.
Anything that follows semi-colon (;) is ignored by the assembler. It is called a comment. Comments
are used to make your programs readable. You use them to explain what you are doing in English.
Parts of assembly language line code
Label : opcode operands ; comments
Start : mov ah,90h ; moving 90h value to ah register
String : db “Hello World ! ”, Ox0A
Length : equ 13
you can also find instruction like
cls ;only mnemonics/opcode
inc ah ; opcode and reg
mov ah,90h ; opcode reg and immediate value

Registers used in Assembly Language

2
GENERAL PURPOSE REGISTERS
8086 CPU has 8 general purpose registers, each register has its own name:
AX - the accumulator register (divided into AH / AL):
1. Generates shortest machine code
2. Arithmetic, logic and data transfer
3. One number must be in AL or AX
4. Multiplication & Division
5. Input & Output

BX - the base address register (divided into BH / BL). CX - the count register (divided into CH /
CL):
1. Iterative code segments using the LOOP instruction
2. Repetitive operations on strings with the REP command
3. Count (in CL) of bits to shift and rotate

DX - the data register (divided into DH / DL):


1. DX:AX concatenated into 32-bit register for some MUL and DIV operations
2. Specifying ports in some IN and OUT operations

SI - source index register:


1. Can be used for pointer addressing of data
2. Used as source in some string processing instructions
3. Offset address relative to DS

DI - destination index register:


1. Can be used for pointer addressing of data
2. Used as destination in some string processing instructions

3
3. Offset address relative to ES
BP - base pointer:
1. Primarily used to access parameters passed via the stack
2. Offset address relative to SS
SP - stack pointer:
1. Always points to top item on the stack
2. Offset address relative to SS
3. Always points to word (byte at even address)
4. An empty stack will had SP = FFFEh

SEGMENT REGISTERS
CS - points at the segment containing the current program.
DS - generally points at segment where variables are defined.
ES - extra segment register, it's up to a coder to define its usage.
SS - points at the segment containing the stack. Although it is possible to store any data in the
segment registers, this is never a good idea. The segment registers have a very special purpose -
pointing at accessible blocks of memory. Segment registers work together with general purpose
register to access any memory value. For example if we would like to access memory at the physical
address 12345h(hexadecimal), we could set the DS = 1230h and SI = 0045h. This way we can access
much more memory than with a single register, which is limited to 16 bit values. The CPU makes a
calculation of the physical address by multiplying the segment register by 10h and adding the general
purpose register to it (1230h * 10h + 45h = 12345h):

The address formed with 2 registers is called an effective address. By default BX, SI and DI
registers work with DS segment register; BP and SP work with SS segment register. Other general
purpose registers cannot form an effective address. Also, although BX can form an effective address,
BH and BL cannot.
SPECIAL PURPOSE REGISTERS
IP - the instruction pointer:
1. Always points to next instruction to be executed
2. Offset address relative to CS

IP register always works together with


CS segment register and it points to currently executing instruction.

FLAGS REGISTER
Flags Register - determines the current state of the processor. They are modified automatically by
CPU after mathematical operations, this allows to determine the type of the result, and to determine
conditions to transfer control to other parts of the program. Generally you cannot access these
registers directly.

4
1. Carry Flag (CF) - this flag is set to 1 when there is an unsigned overflow. For example when
you add bytes 255 + 1 (result is not in range 0...255). When there is no overflow this flag is set to 0.
2. Parity Flag (PF) - this flag is set to 1 when there is even number of one bits in result, and to 0
when there is odd number of one bits.
3. Auxiliary Flag (AF) - set to 1 when there is an unsigned overflow for low nibble (4 bits).
4. Zero Flag (ZF) - set to 1 when result is zero. For non-zero result this flag is set to 0.
5. Sign Flag (SF) - set to 1 when result is negative. When result is positive it is set to0. (This flag
takes the value of the most significant bit.)
6. Trap Flag (TF) - Used for on-chip debugging.
7. Interrupt enable Flag (IF) - when this flag is set to 1 CPU reacts to interrupts from external
devices.
8. Direction Flag (DF) - this flag is used by some instructions to process data chains, when this flag
is set to 0 - the processing is done forward, when this flag is set to 1the processing is done backward.
9. Overflow Flag (OF) - set to 1 when there is a signed overflow. For example, when you add bytes
100 + 50 (result is not in range -128...127).
ASCII Table

5
Assembling and Running Assembly Language Programs
An assembly language program must be assembled and linked before it can be executed. The
assembler produces an object file (extension .OBJ). This file is taken by the linker and an
executable program (extension .EXE) is produced, assuming there were no errors in the program.
We use the MASM assembler and the LINK linker. Create a program called TEST.ASM by copying
the program text and using Notepad (or Edit) save the file.

When saving the file with Notepad, you MUST save it with the “File Type” set to “All Files”.
Running your program
First: Assemble it using MASM
masm test.asm

6
MASM /L TEST.ASM If you have any errors after issuing the MASM command, then you will need
to fix your .ASM file and redo the above steps. You can open the file TEST.LST to see where all
your errors are, but you must make changes the TEST.ASM file, not the TEST.LST file.
Assembler creates object code by translating assembly instruction mnemonics into opcodes, and by
resolving symbolic names for memory locations and other entities
Secondly: Link it using Link
Having successfully assembled your program, as above, you now link it in a similar fashion:
link test.obj

A linker or link editor is a program that takes one or more objects generated by a Assembler and
combines them into a single executable program.

To run your program, simply enter its name:


Test.exe
X

7
This program displays letter A on the screen. Note that only one modification is required in the DX
register.

Lab Tasks:
Try using the following commands one by one and check what is displayed in the output:
mov dx , 41h
mov dx , 65
mov dx , 61h
mov dx , 97
mov dx , 31h

Home Tasks:
Modify to show character S on the console.
Modify to asm code show word 2k10 on the console.

Code View Debugger


In your MASM folder in this case “C:\masm615\BIN” find CV.EXE CV.exe is a code view window
helps to debug your ASM code. Use F10 you can trace into the program for stepwise execution.

8
Find the program test.exe and open it. Use F10 for step-wise execution.

OR you can simply type cv test.exe and you’ll get the required window!
->IF you want to see 32 bit registers Options>Preferences> Check 32-Bit Registers

Das könnte Ihnen auch gefallen