Download presentation
Presentation is loading. Please wait.
Published byAshlynn Strickland Modified over 8 years ago
1
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA 1 Assembly Language Programming
2
2 The language understand by the processor is the machine language(i.e. 1’s and 0’s) but it is almost impossible to write programs directly in machine language. The assembly language is a low-level language in which each of the basic operations that can be performed by the processor are described with alphanumeric symbols instead of with 1’s and 0’s Each instruction of the instruction set is represented by one assembly language statement in a program. Assembly is faster than high level languages Not a case sensitive language. Introduction 2
3
3 Assembly Directives 3 Directives are commands that are part of the assembler syntax but not related to the processor instruction set. Directives act only during the assembling of a program and generate no machine –executable code.
4
4 Assembly in modern computers 4 We can program 8086 microprocessor or any other early microprocessor by using Pentium iii or Pentium iv because every *86 microprocessor build on the basis of 8086 To program in 8086 mode Pentium iii or Pentium iv processor work as virtual 8086 mode In modern P iv computer 1 GB Memory 1MB
5
5 Format of Assembly Program 5 1.Set Virtual 8086 microprocessor At the beginning of the program if we type.286,then it will set virtual 80286 If we do not type anything then it will set virtual 8086 -8086.28680286.38680386 2.Organizing the memory 8086 has 4 memory segments. Each of the segment is capable of addressing maximum of 64 KB of memory Syntax Segment ends.
6
6 Format of Assembly Program … 6 Segment,ends= two directives and always used as a part =any given name to identify the segment =specifies how the segment is aligned with byte,word or paragraph boundary para-paragraph alignment(default),start address is evenly divisible by 16 byte-segment can start at any address word-segment must start on a word =used to indicate how we would like logical segments to be combined to form a physical segment public-allow segment with the same name to be combined common-allow segment with same name to be overlapped stack-allow segments used as stack to be overlapped
7
7 Format of Assembly Program … 7 =A character string that can be used to inform the linker that segments with the same class name should be located close to each other in physical memory For stack segment ‘stack’ For code segment ‘code’ For data segment ‘data’ align_type,combine_type and class_name are optional
8
8 Format of Assembly Program … 8 e.g.- Stack Segment sseg segment para stack ‘stack’ sseg ends Data Segment dseg segment para public ‘data’ dseg ends Code Segment cseg segment para public ‘code’ cseg ends.
9
9 Format of Assembly Program… 9 3.Allocate memory for segments Here we only need to allocate memory to stack segment. sseg segment para stack ‘stack’ db 100 dup(?)processor allocate 100 bytes to stack sseg ends. Type of memory location DirectiveDescription BytedbAllocate one byte in the memory WorddwAllocate two consecutive bytes in the memory Double WordddAllocate four consecutive bytes in the memory
10
10 Format of Assembly Program… 10 4.Coding in the code segment I.Mapping to segment registers The processor use SS register to address stack segment, DS register to address data segment and CS register to code segment. We need to specify the purpose of each segment. assume ss:sseg,ds:dseg,cs:cseg II.Begin the main main: label end main III.Loading three segments “Assume” is just a directive. Therefore we need to write instructions to physically load an address in code segment register
11
11 Format of Assembly Program… 11 We only need to load data segment. other segments will loaded by the operating system To load data segment we must state, ds=dseg mov ds,dseg Invalid mov ax,dseg mov ds,ax
12
12 Format of Assembly Program… 12 IV.Program Instruction The instructions that we have to perform in our program should come next 5.Terminate the Program Assembly is a low level language. Therefore after executing the instruction we need to write instructions to terminate the programs. For that we can use interrupts. Once an interrupt is found the microprocessor stops it execution and transfer control to a new program called interrupt service routine. handle by BIOS Interrupts handle by DOSuse for terminate MOV ah,4ch INT 21h
13
13 Format of Assembly Program… 13 6.Ending the code segment and main cseg ends end main Example Addition of 10 and 20 which is stored in memory. sseg segment para stack ‘stack’ db 100 dup(?) sseg ends dseg segment para public ‘data’ a db 10 b db 20 dseg ends
14
14 Format of Assembly Program… 14 cseg segment para public ‘code’ assume ss:sseg,ds:dseg,cs:cseg main:mov ax,dseg mov ds,ax mov bl,a mov bh,b add bl,bh mov ah,4ch int 21h cseg ends end main
15
15 Defining Memory Variables 15 db E.g. a db 100 db 0-(2 8 -1) dw0-(2 16 -1) dd 0-(2 32 -1) Name of the variable dw dd 100 a1 byte
16
16 Defining Memory Variables … 16 If we need to store a number where base is not equal to 10 we need to use an indicator BaseIndicatorExample 2B1101B 8O456O 10D1234 16H1234H
17
17 Data Movement Instructions 17 Syntax: mov dest,source e.g. mov register,register mov memory,register mov register,memory mov register,immediate(constant) mov memory,immediate Both source and destinations cannot be memory variables.
18
18 Arithmetic Instructions 18 Addition and subtraction Syntax: add dest,source sub dest,source Add dest=dest+source Sub dest=dest-source Both source and destination cannot be memory variable Increment and Decrement Syntax: inc source dec source inc source=source+1 dec source=source-1 Source can be register/memory variable
19
19 Arithmetic Instructions… 19 Multiplication 8-bit multiplication 16-bit multiplication Syntax: mul multiplier 8-bit multiplication Multiplicand* Multiplier=Product AL register(Byte)Byte memory AX register variable or register (word) AHAL Multiplicand AX Product Before After
20
20 Arithmetic Instructions… 20 16-bit multiplication Multiplicand* Multiplier=Product AX register(Word)word memory DX:AX register variable or register (Double word) DX AX Multiplicand DX AX High Product Low Product Before After
21
21 Arithmetic Instructions… 21 Division 8-bit division 16-bit division Syntax: div divisor 8-bit division Dividend/ Devisor=Quotient Remainder AX register(Word)Byte memory AL register AH register variable or register AX Dividend AHAL Remainder Quotient Before After
22
22 Arithmetic Instructions… 22 16-bit division Dividend/ Devisor=Quotient Remainder DX:AX registersword memory AX register DX register (Double word)variable or register DX AX High Devidend Low Devidend DX AX Remainder Quotient Before After
23
23 Define Arrays 23 There are two methods (1) db dw dd e.g.ARR db 1,2,3,4,5 ARR dw 300,400,500 (2)Using dup operator db dup dw dd db dup dw dd
24
24 Define Arrays… 24 1.To define an empty array ARR db 5 dup(?) ARR db 5 dup(?,?,?,?,?) ARR db ????? 2.To define array with same element ARR db 5 dup(1) ARR db 5 dup(1,1,1,1,1) ARR db 1,1,1,1,1 3.To define array with different element ARR db 5 dup(1,2,3,4,5) ARR db 1,2,3,4,5
25
25 Storing Strings/Characters 25 db directive is the only data defining directive that allows to use arbitrary length character string e.g. 1.msg db ‘HELLO’ msg db 5 dup(‘HELLO’) 2.msg db ‘HELLO WORLD’ 3.msg db ‘DON’’t’ O L L E H ASCII value of H
26
26 Looping in Instructions 26 JMP Instruction A commonly used instruction for transferring control (looping) is the jump (JMP) instruction. A jump is unconditional since the operation transfers control under all circumstances. The general format is: JMP OVER OVER:
27
27 Looping in Instructions 27 Loop Instruction Another frequently used loop is the counting loop. This loop repeats a number of instructions a fixed number of times. It uses the value contained in the CX register as a loop counter. E.g. MOV CX, 5 NEXT: MOV BX, OFFSET ARR MOV AL, [BX] LOOP NEXT This loop goes 5 times.
28
28 Looping in Instructions 28 A special comparison instruction (CMP) works almost like the SUB. Both register and variables can be used for the destination and register, variable and constants can be used for the source operands. After comparing we can use conditional jump E.g. CMP NUM1, 25 JE EQUAL. EQUAL:.. The jump on equal (JE) command is used here.
29
29 Looping in Instructions 29 The assembler supports variety of conditional jump instructions that transfer control depending on settings in the flag register. For example, you can compare two fields and then jump according to flag values that the compare sets. The following instructions can be used for unsigned numbers <JB jump on below <= JBE jump on below or equal = JE jump on equal >= JAE jump on above or equal > JA jump on above not =JNE jump on not equal
30
30 Looping in Instructions 30 The following instructions can be used for signed numbers < JL jump on less <= JLE jump on less or equal = JE jump on equal >= JGE jump on greater or equal > JG jump on greater not = JNE jump on not equal
31
31 Looping in Instructions 31 Write a program to calculate the total of each pair of elements in 2 byte arrays. The arrays should be defined in the data segment having a length of your choice. Store the result in the first array. Make sure the sum of each pair is less than 256. ;stack segment sseg segment para stack 'stack' db 100 dup(?) sseg ends ;data segment dseg segment para public 'data' ; defining 2 byte arrays arr1 db 1,2,3,4,5 arr2 db 2,4,6,8,10 dseg ends
32
32 Looping in Instructions 32 ;code segment cseg segment para public 'code' assume ss:sseg, ds:dseg, cs:cseg ; mapping the segments main: mov ax,dseg ; loading the data segment mov ds,ax ; using index memory addressing mode mov si,0 ; store the index zero in SI register mov cx,5 ; store the number of iterations in CX register mov ax,0 ; initializing AX to zero next: mov al,arr2[si] ; copy value of arr2 element in to AL register add arr1[si],al ; add two values in arr1 and AL register inc si ; increase the value of index by one loop next ; direct to the "next" label position
33
33 Looping in Instructions 33 ; terminate the program mov ah,4ch int 21h cseg ends end main
34
34 Looping in Instructions 34 1.Write a program to calculate the total of a byte array. The array should be defined in the data segment having a length of your choice. Store the result in any data register. Make sure the sum is less than 65,536. (Using both register indirect memory addressing mode and index memory addressing mode.) 2.The following are the marks of 10 students. 85, 56, 76, 45, 30, 69, 90, 25, 65, 62 The grades are assigning as follows. If marks >= 75 then grade = A If marks >= 65 then grade = B If marks >= 45 then grade = C If marks < 45 then grade = F Store these marks in an array defined in the data segment and fill another array with the grade of each student in order. Quiz02
35
35 Interrupts 35 An interrupt is an event that causes the processor to suspend its present task and transfer the control to a new program called the interrupt service routine (ISR). There are three types of interrupts: 1. Processor Interrupts These interrupts are generated by the processor itself, usually in response to an error condition. E.g. Interrupt 0 is generated in attempt to divide by zero. 2. Software Interrupts These are the interrupts used in programming. 3. Hardware Interrupts These are interrupt requests initiated by external hardware.
36
36 Interrupts… 36 Interrupt Vector Table BIOS and MS-DOS establish a table called interrupt vector table (IVT), in memory locations 00000H to 003FFH. The BIOS handles interrupts 00H – 1FH and DOS handles interrupts 20H – 30H. Each interrupt must supply a type number which is used by the processor as a pointer into the IVT to determine the address of that interrupt’s service routine (ISR). Each entry in the IVT points to the location of its corresponding ISR. Software Interrupts Each software interrupt provides access to a particular BIOS service or a MS-DOS function. A software interrupt can be call using the INT instruction.
37
37 Interrupts… 37 The 21H Interrupt The 21H DOS interrupt provide four important services as shown in the following table. Note: Using a service of an interrupt is analogous to calling a method of a class of a high level programming language.
38
38 Interrupts… 38 Display an ASCII character 09H Service can be used to display a character on screen. The following instructions should be executed. MOV AH, 02H MOV DL, INT 21H The service prints the character in the DL register.
39
39 Interrupts… 39 Display String 09H Service scan be used to display a character string on screen. The following instructions should be executed. MOV AH, 09H MOV DX, OFFSET INT 21H The service prints the character string that starts from the offset stored in the DX register. The character string should be terminated by a $ sign. It indicates the end of the string.
40
40 Interrupts… 40 E.g. - MSG DB “HELLO WORLD!$” MOV AH, 09H MOV DX, OFFSET MSG INT 21H To add new line character before or after the string use 0AH, 0DH, E.g. - MSG DB 0AH, 0DH,“HELLO WORLD!$”
41
41 Interrupts… 41 Get keyboard inputs 01H Service can be used to get a keyboard input from the user. The following instructions should be executed MOV AH, 01H INT 21H Now the user can input a character from the keyboard. The entered character will be stored in the AL register. Here the user can enter only one character. The character goes to the program in ASCII format. Therefore, if any arithmetic operation is performed on the character it should be converted to its decimal equivalent by subtracting 48 (ASCII 0).
42
42 Interrupts… 42 SUB AL,’0’; ‘0’ represents ASCII 0 E.g. If the user enters 2, the program will take it as ASCII 2 (or 50 in decimal) and hence 48 should be subtracted before the arithmetic instruction
43
43 Interrupts… 43 Buffered key board input using 0AH DOS Service of Interrupt 21H 0AH Service accepts data from a keyboard in a powerful manner. The input area requires a parameters list (memory buffers) containing specified fields that the INT operation is to process. The following information needed. Maximum length of the input data. The actual number of characters entered
44
44 Interrupts… 44 Structure of a memory buffer BUFF LABEL byte;start of the buffer MAXLEN DB 20;maximum number of input characters ACTLEN DB ?;actual number of input characters MSG DB 20 DUP(?) ;character entered from the keyboard LABEL directive tells the assembler to align on a byte boundary and gives the location the name "BUFF". Since "LABEL" takes no space therefore BUFF and MAXLEN refer to the same memory location. To request input following instructions should be executed. MOV AH,0AH MOV DX,OFFSET BUFF INT 21H The INT operation waits for a user to enter characters ad checks that they do not exceed the maximum in the parameter list.
45
45 Interrupts… 45 The user presses the "Enter" key to signal the end of entry. This operation transfers this character (0DH) to the input field, but does not include in the actual length. E.g.- If key in BROWN,the buffer appears like this ASCII205BROWN# HEX140542524F574E0D
46
46 Interrupts… 46 Terminate the program 4CH Service can be used to terminate a program. The following instructions should be executed. MOV AH,4CH INT 21H
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.