MICRO
Unit-2Addressing Modes Q1) Explain the instruction syntax?An 8051 Instruction consists of an Opcode (short of Operation – Code) followed by Operand(s) of size Zero Byte, One Byte or Two Bytes.The Op-Code part of the instruction contains the Mnemonic, which specifies the type of operation to be performed. All Mnemonics or the Opcode part of the instruction are of One Byte size.Q2) Explain the 8051 data types?C language supports 2 different type of data types:Primary data types: These are fundamental data types in C namely integer(int), floating point(float), character(char) and void.2. Derived data types:Derived data types are nothing but primary datatypes but a little twisted or grouped together like array, structure, union and pointer. Data type determines the type of data a variable will hold. If a variable x is declared as int. it means x can hold only integer values. Every variable which is used in the program must be declared as what data-type it is. Integers:Integers are used to store whole numbers. Size and range of Integer type on 16-bit machine:
Char type Character types are used to store characters value.Size and range of Integer type on 16-bit machine
void typevoid type means no value. This is usually used to specify the type of functions which returns nothing Q3) Explain the advantages of subroutines in 8051?Subroutine is a group of instructions written separately from the main program to perform a function that occurs repeatedly in the main program. When subroutine is called ,the current program is stopped and program counter PC is loaded with the memory location of the subroutine, running up to the RET instruction where we produce a return to the main program resumes.It performs specific functions and do not operate on their own. They are always linked to a major program or other subroutines. It can be called many times as necessary as it reduces the code the program to have the effect of code reuse.It Allow the division of program blocks as perform the function of structure. The Instructions to the Subroutine in 8051 Microcontroller is ACALL LCALL RET RETI. Q4) Explain Immediate and Register Addressing Modes?Immediate AddressingIn this addressing mode the data is provided as a part of instruction itself. That is the data immediately follows the instruction. Eg. MOV A,#30H ; ADD A, #83 ; Here # Symbol indicates that data is immediate. Register addressing. In this addressing mode the register will hold the data. One of the eight general registers (R0 to R7) is used and specified as the operand. Eg. MOV A,R0 ; ADD A,R6 R0 – R7 will be selected from the current selection of register bank. The default register bank will be bank 0. Q5) Explain direct and indirect addressing mode?Direct addressing There are two ways to access the internal memory that is by using direct address and indirect address. In direct addressing mode we not only address the internal memory but SFRs also. In direct addressing, an 8 bit internal data memory address is specified as part of the instruction and hence, it can specify the address only in the range of 00H to FFH. In this addressing mode, data is obtained directly from the memory. Eg. MOV A,60h ADD A,30h Indirect addressing The indirect addressing mode uses a register to hold the actual address that is used in data movement. Registers R0 and R1 and DPTR are the only registers that can be used as data pointers. Indirect addressing cannot be used to refer to SFR registers. Both R0 and R1 can hold 8 bit address and DPTR can hold 16 bit address. Eg. MOV A,@R0 ADD A,@R1 MOVX A,@DPTR Q6) Explain Indexed and Relative Addressing?Indexed addressingIn indexed addressing, either the program counter (PC), or the data pointer (DTPR)—is used to hold the base address, and A is used to hold the offset address. Adding the value of the base address to the value of the offset address forms the effective address.Effective address = Base address + value of offset address. Indexed addressing is used with JMP or MOVC instructions. Look up tables are easily implemented with the help of index addressing. Eg. MOVC A, @A+DPTR // copies the contents of memory location pointed by the sum of the accumulator A and the DPTR into accumulator A. MOVC A, @A+PC // copies the contents of memory location pointed by the sum of the accumulator A and the program counter into accumulator A. Relative AddressingRelative addressing is used only with conditional jump instructions. The relative address, (offset), is an 8 bit signed number, which is automatically added to the PC to make the address of the next instruction. The 8 bit signed offset value gives an address range of +127 to —128 locations. The jump destination is usually specified using a label and the assembler calculates the jump offset accordingly.The advantage of relative addressing is that the program code is easy to relocate and the address is relative to position in the memory. Eg. SJMP LOOP1 JC BACK Q7) Explain Absolute Addressing and Long Addressing?Absolute addressing Absolute addressing is used only by the AJMP (Absolute Jump) and ACALL (Absolute Call) instructions. These are 2 bytes instructions. The absolute addressing mode specifies the lowest 11 bit of the memory address as part of the instruction. The upper 5 bit of the destination address are the upper 5 bit of the current program counter. Hence, absolute addressing allows branching only within the current 2 Kbyte page of the program memory. Eg. AJMP LOOP1 ACALL LOOP2 Long Addressing The long addressing mode is used with the instructions LJMP and LCALL. These are 3 byte instructions. The address specifies a full 16 bit destination address so that a jump or a call can be made to a location within a 64 Kbyte code memory space. Eg. LJMP FINISH LCALL DELAY Q8) Explain the data transfer Instructions?In this group, the instructions perform data transfer operations of the following types. a. Move the contents of a register Rn to A MOV A,R2 MOV A,R7 b. Move the contents of a register A to Rn i. MOV R4,A ii. MOV R1,A c. Move an immediate 8 bit data to register A or to Rn or to a memory location(direct or indirect) i. MOV A, #45H ii. MOV R6, #51H i iii.MOV 30H, #44H iv. MOV @R0, #0E8H v. MOV DPTR, #0F5A2H vi. MOV DPTR, #5467H d. Move the contents of a memory location to A or A to a memory location using direct and indirect addressing i. MOV A, 65H ii. MOV A, @R0 iii.MOV 45H, A iv. MOV @R1, A e. Move the contents of a memory location to Rn or Rn to a memory location using direct addressing i. MOV R3, 65H ii. MOV 45H, R2 f. Move the contents of memory location to another memory location using direct and indirect addressing i. MOV 47H, 65H ii. MOV 45H, @R0 g. Move the contents of an external memory to A or A to an external memory i. MOVX A,@R1 ii. MOVX @R0,A iii. MOVX A,@DPTR iv. MOVX@DPTR,A h. Move the contents of program memory to A i. MOVC A, @A+PC ii. MOVC A, @A+DPTR Q9) Explain the arithmetic instructions?The 8051 can perform addition, subtraction. Multiplication and division operations on 8 bit numbers. Addition In this group, we have instructions to i. Add the contents of A with immediate data with or without carry. i. ADD A, #45H ii. ADDC A, #OB4H ii. Add the contents of A with register Rn with or without carry. ADD A, R5 ADDC A, R2 Add the contents of A with contents of memory with or without carry using direct and indirect addressing Subtraction In this group, we have instructions to i. Subtract the contents of A with immediate data with or without carry. i. SUBB A, #45H ii. SUBB A, #OB4H ii. Subtract the contents of A with register Rn with or without carry.SUBB A, R5 SUBB A, R2 Subtract the contents of A with contents of memory with or without carry using direct and indirect addressing Multiplication MUL AB. This instruction multiplies two 8 bit unsigned numbers which are stored in A and B register. After multiplication the lower byte of the result will be stored in accumulator and higher byte of result will be stored in B register. Eg. MOV A,#45H ;[A]=45H MOV B,#0F5H ;[B]=F5H MUL AB ;[A] x [B] = 45 x F5 = 4209 ;[A]=09H, [B]=42H Division DIV AB. This instruction divides the 8 bit unsigned number which is stored in A by the 8 bit unsigned number which is stored in B register. After division the result will be stored in accumulator and remainder will be stored in B register. Eg. MOV A,#45H ;[A]=0E8H MOV B,#0F5H ;[B]=1BH DIV AB ;[A] / [B] = E8 /1B = 08 H with remainder 10H ;[A] = 08H, [B]=10H DA A (Decimal Adjust After Addition). When two BCD numbers are added, the answer is a non-BCD number. To get the result in BCD, we use DA A instruction after the addition. DA A works as follows. If lower nibble is greater than 9 or auxiliary carry is 1, 6 is added to lower nibble If upper nibble is greater than 9 or carry is 1, 6 is added to upper nibble. Eg 1: MOV A,#23H MOV R1,#55H ADD A,R1 // [A]=78 DA A // [A]=78 no changes in the accumulator after DAA Eg 2: MOV A,#53H MOV R1,#58H ADD A,R1 // [A]=ABh DA A // [A]=11, C=1 . ANSWER IS 111. Accumulator data is changed after DAA Q10) Explain the branch instructions ?Jump and Call Program Range There are 3 types of jump instructions. They are:- 1. Relative Jump 2. Short Absolute Jump 3. Long Absolute Jump Relative Jump that replaces the PC (program counter) content with a new address that is greater than (the address following the jump instruction by 127 or less) or less than (the address following the jump
Fig1. Jump instruction The advantages of the relative jump are as follows :- 1. Only 1 byte of jump address needs to be specified in the 2's complement form, ie. For jumping ahead, the range is 0 to 127 and for jumping back, the range is -1 to -128. 2. Specifying only one byte reduces the size of the instruction and speeds up program execution. 3. The program with relative jumps can be relocated without reassembling to generate absolute jump addresses. Disadvantages of the absolute jump: - Short jump range (-128 to 127 from the instruction following the jump instruction) Instructions that use Relative Jump SJMP ; this is unconditional jump The remaining relative jumps are conditional jumps JC <relative address> JNC <relative address>JB bit <relative address> JNB bit <relative address> JBC bit <relative address>CJNE<destination byte> <source byte> <relative address>DJNZ <byte><relative addressJZ<relative address>JNZ<relative address>Short Absolute Jump In this case only 11bits of the absolute jump address are needed. The absolute jump address is calculated in the following manner.In 8051, 64 kbyte of program memory space is divided into 32 pages of 2 kbyte each. The hexadecimal addresses of the pages are given as follows:-
The upper 5bits of the program counter (PC) hold the page number and the lower 11bits of the PC hold the address within that page. Thus, an absolute address is formed by taking page numbers of the instruction (from the program counter) following the jump and attaching the specified 11bits to it to form the 16-bit address. Advantage: The instruction length becomes 2 bytes. Example of short absolute jump: - ACALL , AJMP Long Absolute Jump/Call Applications that need to access the entire program memory from 0000H to FFFFH use long absolute jump. Since the absolute address has to be specified in the op-code, the instruction length is 3 bytes (except for JMP @ A+DPTR). This jump is not re-locatable.Example: - LCALL LJMP JMP @A+DPTR Another classification of jump instructions is 1. Unconditional Jump 2. Conditional Jump 1. The unconditional jump is a jump in which control is transferred unconditionally to the target location. a. LJMP (long jump). This is a 3-byte instruction. First byte is the op-code and second and third bytes represent the 16-bit target address which is any memory location from 0000 to FFFFH eg: LJMP 3000Hb. AJMP: this causes unconditional branch to the indicated address, by loading the 11 bit address to 0 -10 bits of the program counter. The destination must be therefore within the same 2K blocks. c. SJMP (short jump). This is a 2-byte instruction. First byte is the op-code and second byte is the relative target address, 00 to FFH (forward +127 and backward -128 bytes from the current PC value). To calculate the target address of a short jump, the second byte is added to the PC value which is address of the instruction immediately below the jump. 2. Conditional Jump instructions. JBC Jump if bit = 1 and clear bit JNB Jump if bit = 0 JB Jump if bit = 1 JNC Jump if CY = 0 JC Jump if CY = 1 CJNE reg,#data Jump if byte ≠ #data CJNE A,byte Jump if A ≠ byte DJNZ Decrement and Jump if A ≠ 0 JNZ Jump if A ≠ 0 JZ Jump if A = 0 All conditional jumps are short jumps. Bit level jump instructions: Bit level JUMP instructions will check the conditions of the bit and if condition is true, it jumps to the address specified in the instruction. All the bit jumps are relative jumps. JB bit, rel ; jump if the direct bit is set to the relative address specified. JNB bit, rel ; jump if the direct bit is clear to the relative address specified. JBC bit, rel ; jump if the direct bit is set to the relative address specified and then clear the bit.
Type | Size(bytes) | Range |
int or signed int | 2 | -32,768 to 32767 |
unsigned int | 2 | 0 to 65535 |
short int or signed short int | 1 | -128 to 127 |
unsigned short int | 1 | 0 to 255 |
long int or signed long int | 4 | -2,147,483,648 to 2,147,483,647 |
unsigned long int | 4 | 0 to 4,294,967,295 |
Type | Size(bytes) | Range |
char or signed char | 1 | -128 to 127 |
Unsigned char | 1 | 0 t0 255 |
- ADD A, 51H
- ADDC A, 75H
- ADD A, @R1
- ADDC A, @R0 CY AC and OV flags will be affected by this operation.
- SUBB A, 51H
- SUBB A, 75H
- SUBB A, @R1
- SUBB A, @R0 CY AC and OV flags will be affected by this operation.
Page(Hex) | Address(Hex) |
00 | 0000-07FF |
01 | 0800-0FFF |
02 | 1000-17FF |
03 | 1800-1FFF |
……. |
|
1F | F800-FFFF |
0 matching results found