2. Assembly Language is a programming language that is very similar to machine language, but uses symbols instead of binary numbers. It is converted by the assembler into executable machine- language programs. Assembly language is machine-dependent; an assembly program can only be executed on a particular machine.

2.1 Introduction to Assembly Language Tools

Practical assembly language programs can, in general, be written using one of the two following methods: 1- The full-segment definition form 2- The simplified segment definition form In both methods, the source program includes two types of instructions: real instructions, and pseudo instructions. Real instructions such as MOV and ADD are the actual instructions that are translated by the assembler into for execution by the CPU. Pseudo instructions, on the other hand, don’t generate machine code and are only used to give directions to the assembler about how it should translate the assembly language instructions into machine code. The assembler program converts the written assembly language file (called source file) into machine code file (called ). Another program, known as the , converts the object file into an executable file for practical run. It also generates a special file called the map file which is used to get the offset addresses of the segments in the main assembly program as shown in figure 1. Other tools needed in assembling coding include a debugger, and an editor as shown in figure 2

Figure 2. Program Development Procedure There are several commercial assemblers available like the Microsoft Macro Assembler (MASM), and the (TASM). In this experiment, we shall practice using the Microsoft Macro Assembler (MASM).

2.1.1 Assembler An assembler is a program that converts source-code programs written in assembly language into object files in machine language. Popular assemblers have emerged over the years for the family of processors. These include:  Macro Assembler from Microsoft (MASM),  Turbo Assembler from Borland (TASM),  for both Windows and (NASM), and  (GNU assembler distributed by the free software foundation.

We will use MASM 6.15 in this course.

• masm.exe creates an .obj file from an .asm file.

2.1.2 Linker A linker is a program that combines your program's object file created by the assembler with other object files and link libraries, and produces a single executable program. You need a linker utility to produce executable files. Link.exe creates an .exe file from an .obj file. • Use make16.bat to assemble and link a 16-bit format assembly program. • Use make32.bat to assemble and link a 32-bit format assembly program.

2.1.3 Debugger A debugger is a program that allows you to trace the execution of a program and examine the content of registers and memory.

2.1.4 Editor You need a text editor to create assembly language source files. MASM6.15 has its own editor or you can use for example Notepad++.

2.2 Installing and using Microsoft Macro Assembler To learn how to use this software tool we will refer to the video uploaded with this course titled “MASM for 8086” or can also be downloaded from https://www.youtube.com/watch?v=0BSbzjHej-E

2.3 Computer Architecture Overview To write programs in assembly language, there is a need for proper understanding of the basics in computer architecture. Figure 2. shows a simple computer structure which comprise of the followings: • System bus: This connects the various components of a computer. • CPU: This is the heart of the computer where most of computations occur. • RAM: This is a place to where the programs are loaded in order to be executed.

Figure 2. Basic Computer Structure (1)

The Central Processing Unit has several registers as shown in figure 3. The 8086 CPU contains 14 registers. Each register is 16 bits long.

Figure 3. 8086 CPU Architecture

General purpose registers 8086 CPU has 8 general purpose registers namely;  AX - the accumulator register (divided into AH / AL).  BX - the base address register (divided into BH / BL).  CX - the count register (divided into CH / CL).  DX - the data register (divided into DH / DL).  SI - source index register.  DI - destination index register.  BP - base pointer.

 SP - stack pointer.

despite the name of a register, it's the programmer who determines the usage for each general purpose register. The main purpose of a register is to keep a number (variable). the size of the above registers is 16 bit, it's something like: 0011000000111001b (in binary form), or 12345 in decimal (human) form.

4 general purpose registers (AX, BX, CX, DX) are made of two separate 8 bit registers, for example if AX= 0011000000111001b, then AH=00110000b and AL=00111001b. therefore, when you modify any of the 8 bit registers 16-bit register is also updated, and vice-versa. the same is for other 3 registers, "H" is for high and "L" is for low part. because registers are located inside the CPU, they are much faster than memory.

Accessing a memory location requires the use of a system bus, so it takes much longer. Accessing data in a register usually takes no time. therefore, you should try to keep variables in the registers. register sets are very small and most registers have special purposes which limit their use as variables, but they are still an excellent place to store temporary data of calculations.

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 should set the DS = 1230h and SI = 0045h. This is good, since this way we can access much more memory than with a single register that is limited to 16 bit values. CPU makes a calculation of physical address by multiplying the segment register by 10h and adding 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.  - determines the current state of the microprocessor.

IP register always works together with CS segment register and it points to currently executing instruction. flags register is 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, the way you can access AX and other general registers, but it is possible to change values of system registers using some tricks that you will learn a little bit later.

2.4 Instruction Forms: Assembly instructions are made up of an operation code (op-code) and operands. The op-code identifies the action to be taken. The operands identify the source and destination of the data. The operands identify CPU registers, memory locations, or I/O ports. The complete form of an instruction is: op-code destination operand, source operand

For example: INC AX ; one operand (add 1 to register AX) MOV AX, 100 ; two operands (store 100 in register AX) MOV AX, BX ; two operands (move content of register BX into register AX)

Experiment No.1 Addition Aim: - Write assembly language program to perform 8 bit and 16-bit addition

Objective: To add 8 bit and 16 bit binary numbers using addition rules for binary arithmetic instruction.

Software: 8086 Emulator

Theory: The 8086 has four groups of the user accessible internal registers. They are general purpose registers Segment registers pointer and index registers Flag register

General Purpose Registers • AX: Accumulator register consists of two 8-bit registers AL and AH, which can be combined together and used as a 16- bit register AX. AL in this case contains the low-order byte of the word, and AH contains the high-order byte. Accumulator can be used for I/O operations and string manipulation. • BX: Base register consists of two 8-bit registers BL and BH, which can be combined together and used as a 16-bit register BX. BL in this case contains the low-order byte of the word, and BH contains the high-order byte. BX register usually contains a data pointer used for based, based indexed or register indirect addressing. • CX: Count register consists of two 8-bit registers CL and CH, which can be combined together and used as a 16-bit register CX. When combined, CL register contains the low order byte of the word, and CH contains the high order byte. Count register can be used in Loop, shift/rotate instructions and as a counter in string manipulation. • DX: Data register consists of two 8-bit registers DL and DH, which can be combined together and used as a 16-bit register DX. When combined, DL register contains the low order byte of the word, and DH contains the high order byte. Data register can be used as a port number in I/O operations. In integer 32-bit multiply and divide instruction the DX register contains high-order word of the initial or resulting number.

Segment register: • Code segment (CS) is a 16-bit register containing address of 64 KB segment with processor instructions. The processor uses CS segment for all accesses to instructions referenced by instruction pointer (IP) register. CS register cannot be changed directly. The CS register is automatically updated during far jump, far call and far return instructions. • Stack segment (SS) is a 16-bit register containing address of 64KB segment with program stack. By default, the processor assumes that all data referenced by the stack pointer (SP) and

base pointer (BP) registers is located in the stack segment. SS register can be changed directly using POP instruction. • Data segment (DS) is a 16-bit register containing address of 64KB segment with program data. By default, the processor assumes that all data referenced by general registers (AX, BX, CX, DX) and index register (SI, DI) is located in the data segment. DS register can be changed directly using POP and LDS instructions. • Extra segment (ES) is a 16-bit register containing address of 64KB segment, usually with program data. By default, the processor assumes that the DI register references the ES segment in string manipulation instructions. ES register can be changed directly using POP and LES instructions

Pointer and Index Registers

• Instruction Pointer (IP) is a 16-bit register that contains the offset address. IP is combined with the CS to generate the address of the next instruction to be executed. • Stack Pointer (SP) is a 16-bit register pointing to program stack. • Base Pointer (BP) is a 16-bit register pointing to data in stack segment. BP register is usually used for based, based indexed or register indirect addressing. • Source Index (SI) is a 16-bit register. SI is used for indexed, based indexed and register indirect addressing, as well as a source data address in string manipulation instructions. • Destination Index (DI) is a 16-bit register. DI is used for indexed, based indexed and register indirect addressing, as well as a destination data address in string manipulation instructions. Flag Register

Flags is a 16-bit register containing nine 1-bit flags. 06 flags are status flags and 3 are Control Flags • (OF) - set if the result is too large positive number, or is too small negative number to fit into destination operand. • (DF) - if set then string manipulation instructions will auto-decrement index registers. If cleared then the index registers will be auto-incremented. •Interrupt-enable Flag (IF) - setting this bit enables maskable interrupts. •Single-step Flag (TF) - if set then single-step interrupt will occur after the next instruction. •Sign Flag (SF) - set if the most significant bit of the result is set. • (ZF) - set if the result is zero.

•Auxiliary (AF) - set if there was a carry from or borrow to bits 0-3 in the AL register. •Parity Flag (PF) - set if parity (the number of "1" bits) in the low-order byte of the result is even. •Carry Flag (CF) - set if there was a carry from or borrow to the most significant bit during last result calculation. Data Transfer Instructions

Data transfer is one of the most common tasks when programming in an assembly language. Data can be transferred between registers or between registers and the memory. Immediate data can be loaded to registers or to the memory. The transfer can be done on an octet or word. The two operands must have the same size. Data transfer instructions don’t affect the condition indicators (excepting the ones that have this purpose). They are classified as follows: 6. classical transfer instructions 7. address transfer instructions 8. condition indicator transfers instructions 9. input/output instructions (peripheral register transfers)

One of the Classical transfer instructions Include the following instruction:

MOV ,

The MOV instruction is used to transfer a byte or a word of data from a source operand to a destination operand. These operands can be internal registers of the 8086 and storage locations in memory. Mnemonic Meaning Format Operation Flags affected MOV Move MOV D,S (S) → (D) None

Destination Source Example Accumulator Memory MOV AX, TEMP Register Register MOV AX, BX Memory Register MOV COUNT [DI], CX Register Immediate MOV CL, 04

Arithmetic Instructions: Addition

ADD – ADD Destination, Source

These instructions add a number from some source to a number in some destination and put the result in the specified destination.

ADC – ADC Destination, Source The ADC also adds the status of the carry flag to the result. The source may be an immediate number, a register, or a memory location. The destination may be a register or a memory location.

Rules The source and the destination in an instruction cannot both be memory locations. The source and the destination must be of the same type (bytes or words). If you want to add a byte to a word, you must copy the byte to a word location and fill the upper byte of the word with 0’s before adding. Flags affected: AF, CF, OF, SF, ZF.

Examples  ADD AL, 74H; Add immediate number 74H to content of AL. Result in AL  ADC CL, BL; Add content of BL plus carry status to content of CL (CL = CL + BL+ Carry Flag)  ADD DX, BX; Add content of BX to content of DX  ADD DX, [SI]; Add word from memory at offset [SI] in DS to content of DX

Program: - /* 8 BIT ADDITION */ ;8086 PROGRAM TO ADD TWO 8 BIT NUMBERS ; CREATED BY ADEBAYO

DATA SEGMENT N1 DB 01H N2 DB 04H DATA ENDS

CODE SEGMENT ASSUME CS: CODE, DS: DATA START:

MOV AX, DATA MOV DS, AX

MOV AL, N1 MOV BL, N2 ADD AL, BL

ADD AL, 30H MOV DL, AL

MOV AH, 4CH INT 21H

CODE ENDS END START

Observations: -

1. Editing program

2. Using Assembler on the code: this program shows there are no errors

3. Using linker

4. Debugging

RAM before running the code

RAM after running the code

QUESTION

1. What is meant by an ? 2. What is meant by an Operand? 3. What is meant by a Mnemonics? 4. What is the difference between microprocessor and microcontroller? 5. What is meant by LATCH? 6. What is the difference between primary & secondary storage device? 7. What is the difference between static and dynamic RAM? 8. Differentiate between assembler and linker 9. List out the two examples of assembler directives.

References

1. http://www.uobabylon.edu.iq/eprints/publication_1_26408_35.pdf

https://us04web.zoom.us/j/7672735174?pwd=UIBDaGs2VVpjS1E2RUFPV1JWVjNMdz09