Sie sind auf Seite 1von 11

UNIVERSIDAD RICARDO PALMA

INGENIERA MECATRNICA

MICROPROCESADORES Y MICROCONTROLADORES

PRACTICA N 1 The objective of this experiment is to familiarize the students with microprocessor and loading some simple programs. MOV Command: MOV is a basic command to assign a number to a variable. For example following word can be converted to a MOV command as, x = 33; MOV AX,33d (d stays for decimal) here AX stands for x. In this way we can declare variables. MOV can also be used to assign another registers value. x = 33 y=x MOV AX,33 MOV BX,AX MOV can also be used to assign some value to a memory location or from a memory location. MOV AX,[1000H] Storing value to AX from memory location 1000H MOV [1000H],AX Storing value to memory location 1000H from AX MOV [BX],AX Storing value of memory location that has address given by the value of BX, to AX More about MOV: MOV command must be used with registers of equal size. MOV AX,BL is not correct as AX is of 16bits and BL is of 8 bits. Similarly MOV BL,1652h is not correct as 1652h is 16 bit number and available space for BL is only 8 bits. Remember if you write h after a number then it is hexadecimal number. If nothing is written then default type is decimal. NEG command: NEG command is used to get complement of any number. MOV AL,30h NEG AL Now AL will have the value D0h

the 2s

EXERCISE PART 1: Write following codes and perform indicated operations. Take help from previously stated operations for loading and executing the program. a) MOV AX,3012 Observe content of AX register. MOV BX,AX Observe content of BX register. What operation happened here? b) MOV AX,30h; MOV [2010],AX; MOV BX,[2010] Observe content of BX register. What operation happened here? c) MOV SI,1256h MOV [SI],3251h MOV AX,[SI] Observe content of BX register. What operation happened here? d) MOV AL,87h NEG AL NEG AL Write value of AL for every step. ARITHMETIC COMMANDS: ADD, SUB, DIV, MUL are all arithmetic commands. ADD is used to add two numbers. For example following lines can be converted to, x = 1236H y = 1438H z = x+y MOV AX, 1236H MOV BX, 1438H ADD AX,BX This command adds AX and BX content and stores it to AX register. Similarly SUB command is used for subtraction of two numbers. For example, x = 1236H y = 1438H z=xy MOV AX, 1236H MOV BX, 1438H SUB AX,BX AX holds the subtraction result.
2012 I 1

DOCENTE: Ing. Luis Pacheco Cribillero

MICROPROCESADORES

UNIVERSIDAD RICARDO PALMA

INGENIERA MECATRNICA

MICROPROCESADORES Y MICROCONTROLADORES

MUL command is used to multiply two operands. x = 146H y = 5898H z = x*y MOV AX,1456H MOV BX,5898H MUL BX AX holds the lower 16 bit and DX holds the upper 16 bit result. For example result of upper operation is 0709A310. So AX holds A310 and DX holds 0709. DIV is used to perform division operation. x = 5327H y = 15F2H z = x/y MOV AX,5327H MOV BX,15F2H DIV BX AX holds the result and DX holds the remainder. For example for upper operation AX holds 3H and DX holds 1151H. EXERCISE PART 2: Write following codes and perform indicated operations. a) MOV AX,1782H MOV BX,1278H ADD AX,BX Examine register contents b) MOV AX,1782H MOV BX,1278H SUB AX,BX Examine register contents c) MOV AX,2782H MOV BX,1278H MUL BX Examine register contents d) MOV AX,57F2H MOV BX,1375H DIV BX Examine register contents Home Task: 1. Write complete assembly language for the following operation and verify it in DEBUG software.
DOCENTE: Ing. Luis Pacheco Cribillero

x = 30D y = 50D z=x+y w = z*y x = w 20D y = x/5 u = x%5 Clearly indicate which register contains which value clearly. 2. Write a assembly code that will put 1265H to memory location 2100H and 4512H in memory location 1287H. Access these values via registers and perform subtraction operation. 3. Perform Multiplication of 1254H and 4512H. Store the higher 16 bit of the result to CX result. 4. Perform Division operation of 4512H by 1254H. ADD 03H with remainder value.

PRACTICA N 2 Objective: Using logical instructions in assembly language. Incorporating Jump commands in assembly programs. Writing simple assembly language programs with logical and Jump instructions. Introduction: Both logical instructions and Jump commands are widely used in assembly language. These commands are explained below. Logical instructions: Logical instructions include NOT, AND, OR, XOR, TEST etc. instructions. There job is to compare the data values and make results according to logic specified. For example, MOV BX, 30H ; In binary 110000 NOT BX ; In binary 001111 This code takes BX value and then complements all the bits and stores the new value to BX. So it stores 0F value in BX after executing NOT operation. For another example, MOV BX, 70H ; In binary 1110000 MOV CX, 40H ; In binary 1000000 AND CX, BX ; In binary 1000000
2012 I 2

MICROPROCESADORES

UNIVERSIDAD RICARDO PALMA

INGENIERA MECATRNICA

MICROPROCESADORES Y MICROCONTROLADORES

AND operation performs bit by bit AND operation and then stores the value in first operand. In upper code CX holds the final result. MOV BX, 70H ; In binary 1110000 MOV CX, 40H ; In binary 1000000 OR CX, BX ; In binary 1110000 OR operation performs bit by bit OR operation and then stores the value in first operand. In upper code CX holds the final result. Similar case happens for XOR and it is given below, MOV BX, 70H ; In binary 1110000 MOV CX, 40H ; In binary 1000000 XOR CX, BX ; In binary 0110000 Test operation is a little different from AND operation. It performs bit by bit AND operation but it does not change any operands value. MOV BX, 70H ; In binary 1110000 MOV CX, 40H ; In binary 1000000 TEST CX, BX ; In binary CX value is 1000000 All the logical instructions stated above upgrades all the flag register values except AF register. NOT command does not effect any flags. How flags are affected is stated below. MOV BX, 70H ; In binary 1110000 MOV CX, 40H ; In binary 1000000 AND CX, BX ; In binary 1110000 After this operation Zero Flag is 0 (ZF = 0; as the value of CX is not 0), Carry Flag is 0 (CF = 0; as there is no carry), Parity Flag is 0 (PF = 0; as there are odd number of 1s), Sign Flag is 0 (SF = 1), Overflow Flag is 0 (OF = 0; as there is no overflow). In this all the flags can be determined. Do not confuse yourself with semicolon given after each line in assembly codes above. Comments are written after semi colon ; in assembly language. Exercise Part 1: Write following codes and perform indicated operations. (a) Program 1: CODE SEGMENT ASSUME CS:CODE, DS:CODE MOV BX, 3256H MOV CX, 1554H
DOCENTE: Ing. Luis Pacheco Cribillero

AND CX, BX HLT CODE ENDS END Observe content of CX register. What operation happened here? (b) Program 2: CODE SEGMENT ASSUME CS:CODE, DS:CODE MOV BX, 3256H MOV CX, 1554H XOR CX, BX HLT CODE ENDS END Observe content of CX register. What operation happened here? (c) Program 3: CODE SEGMENT ASSUME CS:CODE, DS:CODE MOV AX, 1027H MOV BX, 5A27H MOV CX, 54A5H OR AX, BX XOR AX, CX NOT AX TEST CX, BX AND CX, AX HLT CODE ENDS END Perform this operation in single step mode and write the values of registers for every step. Obtain binary values for upper hexadecimal values and perform bit by bit operation for every step. Compare your hand calculation with obtained result. JUMP Commands: Sometimes it is necessary to go from one line of program to another line without executing some intermediate lines. For this Jump commands are used. We can explain this with a simple example. MOV AX, 3254H MOV BX, 1F4BH MOV CX, 412AH
2012 I 3

MICROPROCESADORES

UNIVERSIDAD RICARDO PALMA

INGENIERA MECATRNICA

MICROPROCESADORES Y MICROCONTROLADORES

ADD AX, CX JMP L3T2 SUB AX, BX L3T2: AND AX, BX HLT In this example L3T2 is a level. As we can see in fifth line JMP command is used. It makes the program to go from fifth line to L3T2 level that is seventh line. So sixth line is not executed. There are two types of Jump commands. These are (i) Conditional jump and (ii) Unconditional Jump. Previous example is an unconditional jump. Conditional Jumps are like if statements. If some flags are affected only then these jump instructions executed. We can look at the following example, MOV AX, 125BH MOV BX, 125BH MOV CX, 412AH SUB AX, BX JZ L3T2 DIV BX L3T2: AND AX,CX HLT Clearly observe the code. In fourth line subtraction operation is performed. As both AX and BX have same value. Their subtracted value is 0. So ZF is set to 1. In fifth line JZ L3T2 is written. It means if ZF = 1 then go to L3T2:. Otherwise continue. As ZF = 1, program moves to eighth line. This is a conditional Jump. Some other conditional jumps are, Command Condition of Jump JA/JNBE CF =0, ZF = 0 JBE/JNA CF = 0 or ZF = 0 JNB/JAE/JNC CF = 0 JB/JNAE/JC CF = 1 JG/JNLE SF OF = 0, ZF = 0 JLE/JNG SF OF = 0, ZF = 1 JGE/JNL SF OF = 0 JL/JNGE SF OF = 1 JZ/JE ZF = 1 JNZ/JNE ZF = 0 JS SF = 1 JNS SF = 0 JPE/JP PF = 1
DOCENTE: Ing. Luis Pacheco Cribillero

JPO/JNP PF = 0 JO OF = 1 JNO OF = 0 JCXZ CX = 0 Exercise Part 2: Write following codes and perform indicated operations. (a) Program 1: CODE SEGMENT ASSUME CS:CODE, DS:CODE MOV AX, 7A24H MOV BX, 15A3H SUB AX, BX JMP L3T2 EEE316: DIV BX JMP Last L3T2: MOV CX, 45B1H AND AX, CX TEST AX, BX JMP EEE316 Last: HLT CODE ENDS END Perform this operation in single step mode and write the values of registers for every step. Explain why we need Last termed level? What will happen if it is not used? (b) Program 1: CODE SEGMENT ASSUME CS:CODE, DS:CODE MOV AX, 7A24H MOV BX, 95A3H ADD AX, BX JC L3T2 EEE316: OR AX, 23H JNZ Last L3T2: MOV CX, 0FC7H SUB AX,CX JZ EEE316 Last: HLT CODE ENDS END Update the register values in every step. Home Task: 1. Write an assembly code that will determine whether a number is greater than 5 or equal of
2012 I 4

MICROPROCESADORES

UNIVERSIDAD RICARDO PALMA

INGENIERA MECATRNICA

MICROPROCESADORES Y MICROCONTROLADORES

less, and put 0 or 1 or 2 for the conditions in DX. 2. Subtract 86B1H from 3F42H and store 0 in CX if overflow occurs and 1 if no overflow occurs. 3. Take 2 arbitrary numbers x and y. If x>1000H perform x+y. If y<1000H perform x-y. If x>1000H and y<100H perform x = x. PRACTICA N 3 Objective: To get familiar with Rotate and Shift commands in assembly language. To use loops in complex problems. Introduction: Shift and Rotate command: Shift and Rotate commands are used to convert a number to another form where some bits are shifted or rotated. Basic difference between shift and rotate is shift command makes fall of bits at the end of register. Where rotate command makes Wrap around at the end of the register. There are both arithmatic (SAL and SAR) and logical (SHL and SHR) shift instructions. Graphical operation for these commands are shown below. Some simple codes can be given to clarify the idea. MOV CL,03H ; MOV AX,02F3H ; In binary 0000 0010 1111 0011 SHR AX,CL ;In binary 0000 0000 0101 1110 In this procedure, SHR commands inserts 0s from right side. Each time a 0 is inserted left most bit is vanished from register content. MOV CL,03H ; MOV AX,82F3H ; In binary 1000 0010 1111 0011 SAR AX,CL ; In binary 1111 0000 0101 1110 In this procedure, SHR commands inserts MSB content from right side. Each time it is inserted left most bit is vanished from register content. MOV CL,03H ; MOV AX,82F3H ; In binary 1000 0010 1111 0011 ROR AX,CL ; In binary 0111 0000 0101 1110 The whole procedure can be visualized as follows.

Here rotate by 3 operation is shown. It is clearly seen that every bit is assigned to a new position that is 3 places away from previous one. Unlike the shift command no right bit is destroyed. It is placed in the leftmost position. Exercise part 1: (a) Program 1: CODE SEGMENT ASSUME CS:CODE MOV CL,02H MOV AX,105AH
DOCENTE: Ing. Luis Pacheco Cribillero MICROPROCESADORES 2012 I 5

UNIVERSIDAD RICARDO PALMA

INGENIERA MECATRNICA

MICROPROCESADORES Y MICROCONTROLADORES

SHL AX,CL HLT CODE ENDS END Obtain AX register value in write the previous value and present value in binary form. What type of operation is this? (b) Program 2: CODE SEGMENT ASSUME CS:CODE MOV CL,04H MOV AX,564AH SAL AX,CL HLT CODE ENDS END Obtain AX register value in write the previous value and present value in binary form. What type of operation is this? (c) Perform for similar values of AX and CL with ROL, ROR. RCL, RCR command. LOOP in assembly language: Loop commands are used to perform same operation again and again. This is like for, while type instructions in C or MATLAB. A common example can be shown as, MOV CX,0100D MOV AX,564AH Lev: DEC AX Loop LEV HLT Here CX acts as a count register. Loop Lev instruction leads instruction to go back to Lev level until CX is zero. Each time Lev level is executed CX is decreased by 1. Loop command can be used for waiting purposes. Such as, MOV CX,0100D Wt: NOP Loop Wt HLT Here the loop is executed until CX is zero. If 1 loop takes 1ms, the program will wait for 100ms.

Exercise part 2: (a) Program 1: CODE SEGMENT ASSUME CS:CODE MOV AX,1025H MOV BX,475AH MOV CX,50H Lev: INC AX DEC BX LOOP Lev HLT CODE ENDS END Observe the operation of this code. What happens when the loop is executed again and again. (b) Program 2: This code is to find GCD (Greatest Common Divisor) CODE SEGMENT ASSUME CS:CODE MOV AX,5H MOV BX,3H Lev: XOR DX,DX DIV BX MOV AX,BX MOV BX,DX TEST DX,0H JNZ Lev HLT CODE ENDS END Here GCD of 5 and 3 are found. You can change the values of AX and BX and obtain the result for any other values. Find GCD of 08D4H and 235H. Result: (c) Find Least Common Multiplier of 12H and 25H. Report: 1. Suppose x = 20 and y = 28. Add y with x for 30 times. 2. Multiply 12 by 6 until result is below 3000H. If result is greater than this, divide the result by 2 for 3 times. 3. You can get input into microprocessor via following code. MOV AH, 1H ; keyboard input subprogram
2012 I 6

DOCENTE: Ing. Luis Pacheco Cribillero

MICROPROCESADORES

UNIVERSIDAD RICARDO PALMA

INGENIERA MECATRNICA

MICROPROCESADORES Y MICROCONTROLADORES

INT 21H HLT Take input from the keyboard until b is pressed. PRACTICA N 4 Objective: To be familiar with stack operations. Calling and executing functions within assembly language programs. Arrays in assembly language programming. Introduction to stack: Stack is a segment where some register values can be stored so that it is not lost. In assembly language programming we have only four registers with which we can perform operations. These are AX, BX, CX and DX. But in many problems we may need a lot of registers. So we need to reuse these registers again and again. This can be done by storing present value of AX in stack segment. Now we can perform other tasks with AX. This will certainly change content of AX. After this getting the value of AX from stack and restoring its previous value. The first job (storing value of AX in stack) is done by PUSH command. Second job (restoring value of AX from stack) is done by POP command in assembly language. A simple program can clarify this. MOV AX, 3256H; Stores 3256H in AX. This is AXs original value PUSH AX; Puts AXs value in stack MOV AX, 1254H; AX is assigned to an intermediate value 1254H DEC AX; New value of AX is 1253H. Certainly AXs value is changed. POP AX; This restores AXs previous value and now AX is 3256H Now how stack segment is organized. Stack segment is a different segment other than default segment 0000 (in which we use to work). It is an array of memory that can store values. Obviously every memory content has an address. Each time a PUSH instruction is called, value of corresponding register is stored in stack memory. If another PUSH is called that corresponding registers value is stored in a
DOCENTE: Ing. Luis Pacheco Cribillero

memory that has address 2 bytes lower than previous one. All these can be shown as, 1. PUSH AX 2. PUSH BX 3. PUSH CX 4. 5. 6. 7. POP CX 8. POP BX 9. POP AX Here the numbers are simply program line numbers. In line 1 AX is PUSHed. Now suppose stack starts with address value 100H. This is called Stack Pointer (SP this indicates where to put a value if something is PUSHed). Now AXs value is stored in 100H address. Stack pointer is changed to 0FEH. This means it is decremented by 2. If BX is PUSHed then its value is stored in 0FEH and new value of SP is 0FCH. Finally if CX is PUSHed its value is stored in 0FCH. All these are shown graphically below.
At the beginning.

After the call, PUSH AX

After the call, PUSH BX

After the call, PUSH CX

When POP command is executed the value stored last in stack, is freed first. Here POP CX has to be called before BX or AX can be
2012 I 7

MICROPROCESADORES

UNIVERSIDAD RICARDO PALMA

INGENIERA MECATRNICA

MICROPROCESADORES Y MICROCONTROLADORES

POPed. POP command automatically increases SP value by 2. So total operation of POP is opposite to PUSH. After the call, POP CX After the POP BX call,

After the call, POP AX

operation. Clearly observe the program and state what happens in every line. CODE SEGMENT ASSUME CS:CODE, DS:CODE MOV AX, 42A6H MOV BX, 2E5AH MOV CX, 12H PUSH AX PUSH BX PUSH CX LEV: ADD AX,BX LOOP LEV POP CX POP BX POP AX HLT CODE ENDS END Introduction to CALL and RET: Like C or MATLAB, in assembly language we can create functions that can be accessed from main program. CALL instruction is needed to access a function. Each function is ended with a RET to go back to main program. Suppose Shatadru is the name of a procedure (in assembly language functions are called procedures). It is called from the main program. What happens is stated step by step below, At the instant of procedure calling,

Exercise Part 1: (a) Program 1: This program stores two values in AX and BX. It finds remainder of division operation between AX and BX, and stores it to BX. AXs value need not to be altered. Clearly observe all the steps and write what happens. CODE SEGMENT ASSUME CS:CODE, DS:CODE MOV AX, 3256H MOV BX, 125AH PUSH AX LEV: SUB AX, BX CMP AX, BX JG LEV MOV BX, AX POP AX HLT CODE ENDS END (b) Program 2: Here BX is added with AX for CX times. It is desired that values of AX, BX, CX will be regained after the complete
DOCENTE: Ing. Luis Pacheco Cribillero

MICROPROCESADORES

2012 I

UNIVERSIDAD RICARDO PALMA

INGENIERA MECATRNICA

MICROPROCESADORES Y MICROCONTROLADORES

When SHATADRU is called next instruction address 0102H is saved in stack. And IP is transferred to 0200H. Remember IP is the address of the code that is executing.

When SHATADRU is finishing IP is set to 0260H

At the instant of procedure calling, When SHATADRU is called next instruction address 0102H is saved in stack. IP is transferred to 0202H.

At the time back in the main program,

When SHATADRU is finished IP is set to 0102H from stack. ,

At the instant of procedure finishing


DOCENTE: Ing. Luis Pacheco Cribillero MICROPROCESADORES 2012 I 9

UNIVERSIDAD RICARDO PALMA

INGENIERA MECATRNICA

MICROPROCESADORES Y MICROCONTROLADORES

Exercise part 2: (a)Multiplication program: Here multiplication is done by shifting and then adding a numbers. This is done as follows. 1101 X1001 1101 0000X 0000XX 1101XXX 1110101 CODE SEGMENT ASSUME CS:CODE, DS:CODE MAIN PROC MOV AX, 123H MOV BX, 16H CALL MULTIPLY MULTIPLY PROC PUSH AX PUSH BX XOR DX, DX REPEAT: TEST BX, 1 JZ END_IF ADD DX, AX END_IF: SHL AX, 1 SHR BX, 1 JNZ REPEAT POP BX POP AX RET MULTIPLY ENDP END MAIN CODE ENDS END (b)Program 2: This calculates GCD of three or more numbers. It uses a procedure that calculated GCD of two numbers, as discussed in the previous experiment. CODE SEGMENT ASSUME CS:CODE, DS:CODE MAIN PROC MOV AX,9H MOV BX,5H MOV CX,4H
DOCENTE: Ing. Luis Pacheco Cribillero

MOV DX,3H CAll GCD MOV BX,CX XCHG AX,BX CAll GCD MOV BX,DX XCHG AX,BX CAll GCD HLT GCD PROC PUSH CX PUSH DX MOV DX,0H LEV: SUB AX,BX CMP AX,BX JG LEV MOV CX,AX MOV AX,BX MOV BX,CX CMP BX,DX JNE LEV POP CX POP DX RET GCD ENDP END MAIN CODE ENDS END Introduction to Arrays in assembly language: Array is nothing but series of data stored in successive locations in memory. It can be showed graphically as,

Simple command to insert an array in assembly language is, W DW 10,20,30,40,50,60 Or MSG DB abcde

MICROPROCESADORES

2012 I

10

UNIVERSIDAD RICARDO PALMA

INGENIERA MECATRNICA

MICROPROCESADORES Y MICROCONTROLADORES

Here DW is data word or word array, DB is data byte or byte array. W and MSG are array names. [W] indicates the starting address. DUP command is used to create an array of same numbers. For example, ALPHA DW 100 DUP(0) Creates an array containing 100 0s. DUP can also be nested. GAMMA DB 5,4, 3 DUP (2, 3 DUP (0), 1) Creates an array of 5,4,2,0,0,0,1,2,0,0,0,1,2,0,0,0,1 Memory access for array is simple. Suppose we have W, an array of 50 elements. We want to swap its 25th value with 31st value. Now W indicates first element of array. W+2 second, W+4 third. So W+48 25th and W+60 31st. So code will be like this. MOV AX, W+48 XCHG W+60, AX MOV W+48, AX Report: 1. Find Least Common Multiplier of four numbers. 2. Perform division operation by shifting. 3. Take an array and fine its mean value.

DOCENTE: Ing. Luis Pacheco Cribillero

MICROPROCESADORES

2012 I

11

Das könnte Ihnen auch gefallen