LABORATORY MANUAL MICROPROCESSOR AND PROGRAMMING ASSEMBLY LANGUAGE 1 STUDENT INFORMATION Name: ____________________________________________ Registration No: ___________________________________ Semester: _________________________________________ Program: _________________________________________ Email Address: ____________________________________ 2 INTRODUCTION What is Assembly Language? A specific set of instructions for a particular computer system. It provides a direct correspondence between symbolic statements and machine language. A programming language with a one-to-one correspondence between its statement and a computer’s machine language Assembly language is called a low-level language because it is close to machine language in structure and function. Each assembly language instruction corresponds to one machine instruction. In contrast, high-level languages such as Pascal, BASIC, FORTRAN and COBOL contain powerful statements that are translated into many machine instructions by the compiler This section is an attempt to provide details on Assembly Language Programming practice. The problems have primarily been divided into 8 sessions covering simple, loops, functions, interrupt handling, calling assembly program from C etc. You must attempt all the problems in the specified number of sessions. In order to complete the tasks as above, you must come prepared with paper-based assembly programs and s h o u l d t e s t t h e m a t t h e c e n t e r f o r a n y p o s s i b l e e r r o r s . P l e a s e n o t e , t h a t t h e a s s e m b l y programs may look cumbersome, but give you a lot of power on machine. They allow you to understand the machine more closely and use it more efficiently. 3 INDEX Lab Session No. 1. Objective Page No. 2. To understand the concept of displaying string in assembly language. 3. To understand the concept of loop in assembly language. 4. To understand the concept of jump instruction in assembly language 5. To understand the concept of new line using carriage return, line feed and macros in assembly language. 6. To understand the concept how to perform arithmetic operations in assembly language 7. Explore debugger using different commands 8. Introduction to MDA – 8086 Training Kit 9. To understand the concept of how to access a memory of trainer 8086 and understand some Assembly instructions. 10. Write a program to display the digits in decimal, from 0-7 on 7-segment. To understand the basic concept and functionality of Assembly Language. 4 Lab Session 01 Object To understand the basic concept and functionality of Assembly Language. Theory ASSEMBLY LANGUAGE Assembly language is a machine specific programming language with a one-to-one correspondence between its statements and the computer’s native machine language. There are many different types of assembly language, each specific to a processor or processor family. IBMPC assembly language refers to instruction recognized by a number of different microprocessors in the Intel family: 8086, 8088, 80186, 80286, 80386, 80486, and Pentium. USES: Assembly language is used most often when either communicating with the operating system or directly accessing computer hardware. Secondly, assembly language is used to optimize certain critical areas of application programs to speed up their runtime execution. ASSEMBLER An assembler is a program that converts source code programs from the assembly language into machine language. The assembler can optionally generate a source- listing file with line numbers, memory addresses, source code statements and a cross-reference listing of symbols and variables used in a program. The most popular assemblers for the Intel family are MASM (Microsoft Assembler), TASM (Turbo Assembler). LINKER A companion program that combines individual files created by an assembler into a single executable file ASSEMBLY PROGRAM SYNTAX Assembly language program consists of statements. 5 A statement is either an instruction to be executed when the program runs or a directive for the assembler. A program normally consists of three parts or segments. DATA SEGMENT Variables are declared in the data segment. Each variable is assigned space in memory and may be initialized. Exp: A DW 3501H It sets memory for a variable called A, and initialize it to 3501H. DW - Define word (16 bits = 2 memory locations) A DW (?) ; un- initialized variable CODE SEGMENT Program instructions are placed in the code segment. Instructions are actually organized into units called procedures. Every procedure starts with a line. Exp: Main Proc; Main is the name of procedure and PROC is the directive identify the start of the procedure Main Endp; Main is again the name of the procedure and Endp is the direcitive ; identifies the end of the procedure STACK SEGMENT The stack segment is used for temporary storage of addresses and data. If no stack segment is declared, an error message is generated, so there must be a stack segment even if the program doesn’t utilize the stack. These segments begin with the directives .stack, .code, and .data PROGRAM SYNTAX TITLE first program syntax .Model Small ;Specifies the memory model used .Stack 100H ;allocate 100H memory locations for stack .Data ;start of the data segment ; Data definitions here A DB ? …….. .Code ;start of the code segment Main Proc ;start of the first procedure ; instructions here …… Main Endp ;end of the first procedure 6 ; Other procedures here End Main ;end of the complete assembly program BASIC DIRECTIVES Following are the description of commonly used directives; The .MODEL directive specifies the memory model for an assembler module that uses the simplified segment directives. The .MODEL directive must precede .CODE, .DATA, and .STACK. Note that near code is branched to (jumped to) by loading the IP register only, while far code is branched to by loading both CS and IP. Similarly, near data is accessed with just an offset, while far data must be accessed with a full segment:offset address. In short, far means that full 32bit segment:offset addresses are used, while near means that 16-bit offsets can be used. The format of the .MODEL directive is: .MODELmemorymodel[[,langtype]] [[,stackoption]] The memorymodel can be TINY, SMALL, COMPACT, MEDIUM, LARGE, HUGE, or FLAT. The langtype can be C, BASIC, FORTRAN, PASCAL, SYSCALL, or STDCALL. The stackoption can be NEARSTACK or FARSTACK. TINY One segment. Thus both program code and data together must fit within the same 64 Kb segment. Both code and data are near. SMALL Program code must fit within a single 64 Kb segment, and data must fit within a separate 64 Kb segment. Both code and data are near. MEDIUM More than one code-segment. One data-segment. Thus code may be greater than 64K. COMPACT One code-segment. More than one data-segment. Thus data may be greater than 64K. LARGE More than one code-segment. More than one data-segment. No. Thus both code and data may be greater than 64K. HUGE More than one code-segment. More than one data-segment. Thus both code and data may be greater than 64K. FLAT No segmentation, all code and data can reach any location up to 4 Gb. 7 All program models but TINY result in the creation of exe-format programs. The TINY model creates com-format programs. .STACK: Defines the size of stack used in program .DATA: Defines the data segments for data used in the program. Mark the beginning of the data segment .CODE: Identifies the code segment which contains all the statements. Also .code marks the beginning of the code segment. PROC: Beginning of the procedure ENDP: End of the procedure END:End of assembly language program BASIC MOV INSTRUCTION: We already defined in the Lab#1 RESTRICTIONS: Move between memory to memory is not allowed. A number directly inside a segment register is not allowed. Segment to segment registers, move is not allowed. The INTerrupt Instruction Pentium processor has two memory architectures: real and protected. In real mode a Pentium works like fast 8086 processor. Real mode uses 16 bit addresses. The Real mode is also called as 16-bit mode, because all 20 bit physical address is constructed by 16 bit address. MS-DOS Operating system was the first operating system to implement Real-Address mode on IBM personal computer. The INT instruction is the instruction which does the most work in any assembler program. INT instruction calls a DOS interrupt service routine (like a function) to perform a special task. INT InterruptNumber Where Interrupt Number ranges from 00H to 0FFH (i.e., from 0 to 255). 8 MS-DOS Operating system provides many common services through INT 21h. INT 21h MS-DOS services are procedures that provide input-output, file handling, and memory management. They are also called “MS-DOS function calls.” The execution of an INT instruction causes an Interrupt Service Routine (ISR) associated with the InterruptNumber to be executed. Many of the ISRs have multiple sub-functions. To specify which sub-function is to be executed under a particular InterruptNumber, the AH register is assigned a sub-function number before the execution of the INT instruction. Example: MOV AH , 02H INT 21H DOS FUNCTION CALLS (INT 21H) DOS function calls preserve the contents of all the registers except the AX register and any other register or registers in which they explicitly return data. TERMINATE PROGRAM AND RETURN TO DOS Every time you want to terminate the program and return to DOS, you have to put the following codes: Assembly Language C Language Meaning mov AX , 4C00H int 21h exit(0) Program terminates normally mov AX, exit(1) Program terminates with error code 1. int 4C01h 21h CHARACTER OUTPUT To display a character, you have to use the DOS function 02h. The Initial requirement The result AH = 02h The character stored in DL will be displayed. DL = Character or ASCII Code 9 Sample Code The following code fragment will display a string 'Hey'. .model small .stack 100h .code Main proc mov DL, 'H' mov AH, 2 int 21h mov DL, 'e' mov AH, 2 int 21h mov AH, 2 mov DL, 'y' int 21h mov ah,4ch int 21h main endp end main Lab Tasks 1. Write a program to display your name. 10 11