2. Assembly Language Assembly Language Is a Programming Language That Is Very Similar to Machine Language, but Uses Symbols Instead of Binary Numbers
Total Page:16
File Type:pdf, Size:1020Kb
2. Assembly Language 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 machine code 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 object file). Another program, known as the linker, 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 Borland Turbo Assembler (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 Intel family of processors. These include: Macro Assembler from Microsoft (MASM), Turbo Assembler from Borland (TASM), Netwide Assembler for both Windows and Linux (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. flags register - 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.