You are on page 1of 27

Assembly Programming

Language (CS318)

Lecture 01: Introduction

Collected and Edited by:


Dr. Mina Younan
Lecturer of Computer Science,
Faculty of Computers and Information, Minia University

21
22
Dr. Mina Younan
Agenda
• Preface
• Basics
• Introduction to Assembly Language
• Main Hardware Components
• Views of Computer Systems
o User view
o Programmer view
o Etc.
• Assembler: Steps to install assembler and run assembly code

21
22
Dr. Mina Younan
Intelligent Systems 2
Preface
• Lab 10
• Oral 10
• Midterm 10
• Periodical Tasks 10
• Final 60

21
22
Dr. Mina Younan
Intelligent Systems 3
Preface
• What is Assembly language?
• Why learn Assembly language?
• What is Machine Language?
• How is Assembly related to Machine language?
• Is Assembly Language portable?

21
22
Dr. Mina Younan
Intelligent Systems 4
Selected Terminologies

Computer Architecture Computer Organization


is concerned with the way hardware
concerned with the structure and behavior of
components are connected together to form a
a computer system as seen by the user.
computer system.
It acts as the interface between hardware and It deals with the components of a connection
software. in a system.
helps us to understand the functionalities of a tells us how exactly all the units in the
system. system are arranged and interconnected.
A programmer can view architecture in terms
expresses the realization of architecture.
of instructions, addressing modes and registers.
While designing a computer system An organization is done on the basis of
architecture is considered first. architecture.
deals with high-level design issues. deals with low-level design issues.
involves Logic (Instruction sets, Addressing involves Physical Components (Circuit design,
modes, Data types, Cache optimization) Adders, Signals, Peripherals)

21
22
Dr. Mina Younan
Intelligent Systems 5
Computer Hardware: Main Components
• CPU contains set of registers.
• Registers are faster than RAM
for transferring data.
• The Model 80386 has 16
application register  which
means they have special
purposes for storing data and
executing instructions.
• Registers have names as
indicated in next slides.
Figure 1-1: Main components grouped by
main category.
• Notes: Assembly codes in this
course will use the architecture
of CPU “.386”, and “.MODEL
FLAT” (32-bit memory).
21
22
Dr. Mina Younan
Intelligent Systems 6
Computer Hardware: Main Components
• Main components are processor, memory and I/O devices.
• Communication between components are handled using: address bus, data
bus, and control bus.

21
22
Dr. Mina Younan
Intelligent Systems 7
Computer Hardware: Main Components
• Main components in processor:

21
22
Dr. Mina Younan
Intelligent Systems 8
Computer Hardware: Main Components
• Main components in processor:
o Registers
o Arithmetic and logic unit (ALU)
o Control Unit: constains instruction register and program counter

21
22
Dr. Mina Younan
Intelligent Systems 9
Computer Hardware: Main Components
• Communication
between ALU and
Registers in
processor

21
22
Dr. Mina Younan
Intelligent Systems 10
Computer Hardware: Main Components
• Processor:
o Execution Cycle
‒ Fetch
‒ Decode
‒ Execute
o Von Neumann Architecture
‒ Stored program model
 No distinction between data and instructions
 Instructions are executed sequentially

21
22
Dr. Mina Younan
Intelligent Systems 11
Computer Hardware: Main Components
• Processor:
o Execution Cycle stages:
‒ Copy memory address from program counter into memory address register (MAR)
‒ Increment address in program counter to fetch next instruction
‒ Processor sends a signal along the address bus to memory address held in the MAR
‒ Then send instruction/data held in that memory address along data bus to memory data
register (MDR).
‒ After that copy instruction/data in MDR into current instruction register (CIR)
‒ Then decode instruction/data in CIR and execute. Result is stored in accumulator(ACC)

21
22
Dr. Mina Younan
Intelligent Systems 12
Computer Hardware: Main Components

• Processor:
o Pipelining is a technique where multiple instructions are overlapped
during execution. Pipeline is divided into stages and these stages are
connected with one another to form a pipe like structure. Instructions enter
from one end and exit from another end.
o Types
‒ Arithmetic Pipeline: Arithmetic pipelines are usually found in most of the
computers. They are used for floating point operations, multiplication of fixed
point numbers etc.
‒ Instruction Pipeline: In this a stream of instructions can be executed by
overlapping fetch, decode and execute phases of an instruction cycle. This
type of technique is used to increase the throughput of the computer system.
An instruction pipeline reads instruction from the memory while previous
instructions are being executed in other segments of the pipeline. Thus we can
execute multiple instructions simultaneously.

21
22
Dr. Mina Younan
Intelligent Systems 13
Computer Hardware: Main Components

• Processor:
o Advantages of Pipelining
‒ The cycle time of the processor is reduced.
‒ It increases the throughput of the system
‒ It makes the system reliable.

21
22
Dr. Mina Younan
Intelligent Systems 14
Computer Hardware: Main Components
What is RISC and CISC Architecture & Their Differences
• The architectural design of the CPU is Reduced instruction set computing
(RISC) and Complex instruction set computing (CISC). CISC has the
capacity to perform multi-step operations or addressing modes within one
instruction set.

21
22
Dr. Mina Younan
Intelligent Systems 15
Computer Hardware: Main Components
What is RISC and CISC Architecture & Their Differences
• Reduced Instruction Set Computer (RISC):
o Uses simple instructions
o Operands are assumed to be in processor registers
‒ Not in memory
‒ Simplifies design (fixed instruction size)
• Complex Instruction Set Computer (CISC):
o Uses complex instructions
o Operands can be in registers or memory
‒ Instruction size varies
o Typically uses a microprogram

21
22
Dr. Mina Younan
Intelligent Systems 16
Views of Computer Systems
• User View

21
22
Dr. Mina Younan
Intelligent Systems 17
Views of Computer Systems
• Programmer View
o Hierarchy of Languages
o Instruction level
‒ level of abstraction
Applications
‒ Specify how a
processor functions Machine-
(define logical independent High-level Languages
processor) Increase
o Various physical level of
implementations are abstraction
possible Machine- Assembly Language
specific
o Examples for instruction
Machine Language
set architecture (ISA):
SPARC and JVM

21
22
Dr. Mina Younan
Intelligent Systems 18
Programming Languages
• Advantages of High-level languages
o Abstraction level  few instructions to code  easy and fast
programming
o fixing error is easier
o Programs are portable
‒ Contains few machine-dependent details
‒ Compilers translate code to a specific machine
• Why we need assembly language
o Efficiency
‒ memory and processing

21
22
Dr. Mina Younan
Intelligent Systems 19
Programming Languages
• Machine Language
o Native to processor
o Consists of 0’s and 1’s
o Example: 101  represent digit ‘5’,
‒ In Hexa-decimal ‘FF060B00’  in case of memory addressing

• Assembly Language
o Slightly higher-level language than Machine language
o Slightly human readable
o One-to-one correspondence to machine language instructions
o Example: inc count

21
22
Dr. Mina Younan
Intelligent Systems 20
Assembly Language
• Assembly Language
o Assemblers that translate assembly code into machine code:
‒ Microsoft Assembler (MASM)
‒ Borland Turbo Assembler (TASM)
‒ The GNU assembler (GAS)
‒ The Netwide Assembler (NASM)
o Compilers in high-level languages can translate high-level code directly
and indirectly by using assembler as intermediate stage.
‒ High level language instruction examples:
 Ex1: x = y+z
 Result++  (in c++)
‒ Equivalent assembly language instructions:
 Ex1: mov eax, x
Add eax, y
 Ex2: inc result
21
22
Dr. Mina Younan
Intelligent Systems 21
Assembly Language
• Assembly Language
o Examples for high level language instructions written in C++ and
corresponding assembly instructions (equivalent segment codes).

21
22
Dr. Mina Younan
Intelligent Systems 22
Assembly Language
• Assembly Language

21
22
Dr. Mina Younan
Intelligent Systems 23
Assembly Language
• For MASM and NASM, it is preferred to install DOSBox and
perform the following steps:
o Copy NASM and MSAM folders into a folder entitled “Assembly” to certain
candidate drive (e.g., ‘F’).
o Write assembly code in a file using any of text editors (e.g., Notepad) – avoid
using word and similar applications due to special characters formats -, change
file extension into “.asm”, then copy/move the file to NASM/MASM folder.
• Assembly code written in NASM: add two numbers.
• ;assembly code to add two numbers
• org 0x100
• mov ax,5
• mov bx,10
• add ax,bx
• mov ax, 0x4c00
• int 0x21
21
22
Dr. Mina Younan
Intelligent Systems 24
Assembly Language
• Assembly code written in MASM: “Hello”
dosseg
.MODEL SMALL
.STACK 100H

.DATA
MSG DB 'HELLO!$'

.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
LEA DX, MSG
MOV AH, 9

INT 21h
MOV AH, 4CH
INT 21h

MAIN ENDP
END MAIN
21
22
Dr. Mina Younan
Intelligent Systems 25
Assembly Language
• Run DOSBox and write the following commands:
o mount drive_letter package_path
o drive_letter:\
o masm:
‒ masm filename.asm
‒ link filename
‒ filename.exe
onasm:
‒ nasm filename.asm -o filename.com
‒ AFD filename.com
‒ App. is loaded in debugger, press F2 to run & monitor registers.

21
22
Dr. Mina Younan
Intelligent Systems 26
Assembly Language

Figure 1-6: DOSBox for MASM Assembler.


21
22
Dr. Mina Younan
Intelligent Systems 27

You might also like