2 Assembly languageAn assembly language is a low-level language for programming computers.It implements a symbolic representation of the numeric machine codes and other constants needed to program a particular CPU architecture. This representation is usually defined by the hardware manufacturer, and is based on abbreviations (called mnemonics) that help the programmer remember individual instructions, registers, etc.An assembly language is thus specific to certain physical or virtual computer architecture (as opposed to most high-level languages, which are usually portable).
3 How to learn programming C –ConceptL – Logic thinkingP – PracticeConcept – we must learn the basic syntax, such as how a program statement is writtenLogic thinking – programming is problem solving so we must think logically in order to derive a solutionPractice – write programs
4 Instruction SetA collection of assembly/machine language instructions that the processor can execute.Types of instructions:Transfer (MOV, PUSH, POP etc)Arithmetic (ADD, SUB, MUL etc)Logical (AND, OR, NOT etcControl Transfer (CALL, JMP etc)Miscelleneous (NOP, INT, LEA)
5 Format of Assembly language Statement General format for an assembly language statementLabel Instruction CommentStart: Mov AX, BX ; copy BX into AXStart is a user defined name.put in a label in your statement when necessary!!!!The symbol : is used to indicate that it is a labelIn instruction, ‘mov’ is operation code (opcode) and ‘AX’ and ‘BX’ are operands.
7 Assignment Statement Assignment In Java, assignment takes the form: x = 42 ;y = 24;z = x + y;In assembly language we carry out the same operation but we use an instruction to denote the assignment operator (“=” in Java).mov x, 42mov y, 24add z, xadd z, y
8 Assignment StatementThe mov instruction carries out assignment in 8086 assembly language.It allows us to place a number in a register or in a memory location (a variable) i.e. it assigns a value to a register or variable.Example: Store the ASCII code for the letter A in register bx.A has ASCII code 65D ( B, 41H)The following mov instruction carries out the task:mov bx, 65d
9 Assignment Statement We could also write it as: mov bx, 41h or mov bx, bmov bx, ‘A’All of the above are equivalent. They each carry out exactly the sametask, namely the binary number representing the ASCII code of A iscopied into the bx register.
10 Assembly program development .asmObject file.objExecutable file.exelinkAssemble
11 Input and Output in 8086 Assembly Each microprocessor provides instructions for I/O with the devices that are attached to it, e.g. the keyboard and screen.The 8086 provides the instructions “in” for input and “out” for output. These instructions are quite complicated to use, so we usually use the operating system to do I/O instead.The operating system provides a range of I/O subprograms, in much the same way as there is an extensive library of subprograms available to the C programmer. In C, to perform an I/O operation, a subprogram is called using its name, e.g. putchar() , printf() , getchar() . In addition, for example the character to be displayed by putchar() is passed as a parameter to the subprogram e.g. putchar(c).
12 Software InterruptsIn assembly language we must have a mechanism to call the operating system to carry out I/O. In addition we must be able o tell the operating system what kind of I/O operation we wish to carry out, e.g. to read a character from the keyboard, to display a character or string on the screen or to do disk I/O.Finally, we must have a means of passing parameters to the operating subprogram.
13 “int” instructionIn 8086 assembly language, operating system subprograms are not called by name, instead, by using a software interrupt mechanism An interrupt signals the processor to suspend its current activity (i.e. running your program) and to pass control to an interrupt service program (i.e. part of the operating system).A software interrupt is one generated by a program (as opposed to one generated by hardware). The 8086 int instruction generates a software interrupt.It uses a single operand which is a number indicating which MSDOS subprogram is to be invoked.When the I/O operation is finished, the interrupt service program terminates and our program will be resumed at the instruction following int.
14 int 21h and its servicesFor I/O and some other operations, the number used is 21h . Thus, the instruction int 21h transfers control to the operating system, to a subprogram that handles I/O operations. This subprogram handles a variety of I/O operations by calling appropriate subprograms.This means that particular I/O operation (e.g. read a character, display a character) must be specified. This is done by placing a specific number in a register. The ‘ah’ register is used to pass this information. For example, the subprogram to display a string is subprogram number 9h.
15 String OutputService 9h to display strings which are terminated by the ‘$’ character.In order to use it, we must:Ensure the string is terminated with the ‘$’ character.Specify the starting address of the string in the dx register.Specify the string output subprogram by storing 9h in ah.Use int 21h to call MS-DOS to execute subprogram 9h.
16 Defining String Variables The following 3 definitions are equivalent ways of defining a string “abc“:style1 db “abc” ; string constantstyle2 db ‘a’, ‘b’, ‘c’ ; character constantsstyle3 db 97, 98, 99 ; ASCII codes
17 A program template.model small .stack 100h .code start: ; < the code/statements are here > mov ax, 4c00h ; return to OS int 21h end start
18 EP#1: A Simple Assembly Program .model small .stack 100h .data mesg db “Welcome to Assembly Language $” .code start: mov mov ds, ax ;initialize data segment mov ah, 9h ; service of interrupt 21 to print a message lea dx, mesg ; load effective address int 21h mov ax, 4c00h ; terminating program service end start end
20 Character OutputThe task here is to display a single character on the screen. There are three elements involved in carrying out this operation using the int instruction:We specify the character to be displayed. This is done by storing the character’s ASCII code in a specific register. In this case we use the dl register, i.e. we use dl to pass a parameter to the output subprogram.We specify which of MS-DOS’s I/O subprograms we wish to use. The subprogram to display a character is subprogram number 2h . This number is stored in the ah register.We request MS-DOS to carry out the I/O operation using the int instruction. This means that we interrupt our program and transfer control to the MS-DOS subprogram that we have specified using the ah register.
21 Character OutputExample : Write a code fragment to display the character ’a’ on the screen:C version:putchar( ‘a’ ) ;8086 version:mov dl, ‘a’ ; dl = ‘a‘mov ah, 2h ; character output subprogramint 21h ; call ms-dos output characterAs you can see, this simple task is quite complicated in assembly language.
22 EP#2: Displaying a character from a register .model small .stack 100h .data msg1 db "Displaying a character from register:$” .code mov mov ds, ax mov ah,9 ; display message mov dx, offset msg1 ; lea dx, msg1 int 21h mov ah,2 ; display character mov dl, 65 mov ax, 4c00h end
23 EP#3: Displaying a character from a register on newline .model small .stack 100h .data msg1 db "Displaying a character from register:”,13,10, ‘$’ .code mov mov ds, ax mov ah,9 ; display message mov dx, offset msg1 ; lea dx, msg1 int 21h mov ah,2 ; display character mov dl, 65 mov ax, 4c00h end
24 Assignment AP#1, AP#2 Write a program to display ‘Salam’ using (a) character output and(b) using string output.Write a program to display the message ‘Ding! Ding! Ding!’ and output ASCII code 7 three times. (ASCII code 7 is the Bel character. It causes your machine to beep! ).
25 Character Input with echo Example : Write a code fragment to input a character from the keyboard:C version:getche( ) ;8086 version:mov ah, 1 ; character input subprogramint 21h ; call ms-dos output characterThe ASCII code of the character will be moved in ‘al’ register.
26 EP#4: Program to read a character and display it .model small .stack 100h .code start: mov ah, 1h ; keyboard input subprogram int 21h ; read character into al mov dl, al mov ah, 2h ; display subprogram int 21h ; display character in dl mov ax, 4c00h ; return to ms-dos int 21h end start
27 Assignment: AP#3Write a program to beep, display ‘?’ as a prompt, read a character and display it on a new line.
28 EP#5: Input character and display with messages Title ask user for 1 character and display it again on screen with appropriate messages . .model small .stack 100h .data msg1 db "Input a character: $" msg2 db 13,10,"You entered: $" .code mov mov ds, ax mov ah,9 ; display message lea dx, msg1 int 21h mov ah, 1 int 21h ; my character in al register mov bh, almov ah,9 lea dx, msg2 int 21h mov ah,2 ; display character mov dl, bh mov ax, 4c00h end
29 Character Input without echo Example : Write a code fragment to input a character from the keyboard without displaying it:C version:getch( ) ;8086 version:mov ah, 8 ; character input subprogramint 21h ; call ms-dos output characterThe ASCII code of the character will be moved in ‘al’ register.
30 Assignment: AP#4Write a program to display a message “Input a secret character:” and then input the character and display the character after displaying the message on newline “hmm.. You actually input:”.