Microprocessor programming basics (some programming instructions)

mp vtu basics

This section covers almost all the command instructions coming up in the 24 programs of the laboratory syllabus. 

Lets get started.

1.MOV instruction:
This copies a data from one place to other.
There are 8 modes of addressing the data using MOV instruction. Out of which only 3, which are important for the programs are discussed here.

  • Register addressing
     Copies a byte or a word from the source register to a destination register
E.g.: MOV CX, DX

  • Immediate addressing
Copies the source which is an immediate byte, word, double-word, or quad-word of data, into the destination register or memory location.
E.g.: MOV AL, 22H   ;22H is a direct value, AL is a register

  • Register indirect addressing
Copies a byte or word between a register and a memory location which is addressed by an index register (SI & DI) or base register (BP or BX)
E.g.: MOV AX, [BX]    ;copies a word sized data from the data segment offset address indexed by BX register into AX register.


Important:
The MOV instruction Can Not,
  1. Set the value of the CS and IP registers  
  2. Copy value of one segment register to another segment register (should copy to general register first).  
  3.  Copy immediate value to segment register (should copy to general register first).




2.MUL instruction:
Does an unsigned multiply operation of the contents in the REG/Memory with contents of AL register.
Algorithm:
When operand is a byte:
AX = AL * operand.
When operand is a word:
(DX: AX) = AX * operand.   


3.CMP instruction:
Compares two fields. Result is not stored anywhere. Instead,flags are set (OF, SF, ZF, AF, PF, CF) according to result.


4.JMP instruction:
Performs an unconditional Jump. Transfers control to another part of the program. Label name can be any valid identifier. 

5.Conditional jumps: 
  • JA - Jump If Above. Short Jump if first operand is Above second operand 
  • JAE - Jump If Above Or Equal Short Jump if first operand is Above or Equal to second operand 
  • JB - Jump If Below. Short Jump if first operand is Below second operand 
  • JBE - Jump If Below Or Equal Short Jump if first operand is Below second operand 
  • JC - Jump If Carry Short Jump if Carry flag is set to 1. 
  • JE - Jump If Equal. Short Jump if first operand is Equal to second operand.  
  • JG - Jump If Greater. Short Jump if first operand is Greater, then second operand. 
  • JGE - Jump If Greater Or Equal. Short Jump if first operand is Greater or Equal to second operand. 
  • JL - Jump If Less than. Short Jump if first operand is Less then second operand.
  • JLE - Jump If Less Or Equal. Short Jump if first operand is Less or Equal to second operand.
  • JNZ - Jump If Non Zero. Short Jump if Not Zero (if result is not equal to zero)
  • JZ - Jump If Zero. Short Jump if Zero (result equal to zero).

6.LEA instruction:
Loads the effective address of a memory area to any register.
Algorithm:
REG = address of memory (offset)  


7.LOOP instruction:
jumps to a defined label by decreasing CX register value by 1 and jumps only if CX not equal to zero.
Algorithm:
CX = CX - 1
if CX != 0 then jump
else, No jump, continue  

8. ADD instruction: 
Adds the value of first and second operand and stores in first operand.

Ex. ADD DX,BX   ;adds BX and DX and stores in DX 

Algorithm:
operand1 = operand1 + operand2 


9. AND instruction: 
Logical AND between all bits of two operands. Result is stored in operand1.
These rules apply: 
  • 1 AND 1 = 1  
  • 1 AND 0 = 0
  • 0 AND 1 = 0
  • 0 AND 0 = 0

10.OR instruction:
Logical OR between all bits of two operands. Result is stored in first operand.
These rules apply: 
  • 1 OR 1 = 1
  • 1 OR 0 = 1
  • 0 OR 1 = 1
  • 0 OR 0 = 0
11.SUB instruction:
Difference between two numbers is stored in the first operand. 

Ex. SUB DX,BX   ;subtracts BX from DX and stores in DX

Algorithm:
operand1 = operand1 - operand2 


12.INC instruction:
Increments the value of the register by 1.
Ex. INC SI  ;increments SI by 1.
Algorithm:
operand = operand + 1

13.DEC instruction:
Decrements the value of the register by 1.
Ex. DEC SI  ;decrements SI by 1.

Algorithm:
operand = operand - 1


14.SHL instruction:
Performs a Shift Left operation. The number of shifts is set by operand2.

Ex. SHL DX,1   ;shifts bits of DX by 1 bit to the left

Algorithm:
Shift all bits left, the bit that goes off is set to CF.
Zero bit is inserted to the right-most position. 

15.SHR instruction:
Performs a Shift Right operation. The number of shifts is set by operand2.

Ex. SHR DX,1   ;shifts bits of DX by 1 bit to the right
Algorithm:
Shift all bits right, the bit that goes off is set to CF.
Zero bit is inserted to the left-most position.

16.ROL instruction:
Rotate bits of operand1 to the left. The number of bit of rotation is set by operand2.
Algorithm:
Shift all bits left, the bit that goes off is set to CF and the same bit is inserted to the right-most position.

17.ROR instruction:
Rotate bits of operand1 to the right. The number of bit of rotation is set by operand2.  
Algorithm:
Shift all bits right, the bit that goes off is set to CF and the same bit is inserted to the left-most position.


18.CALL instruction:
Transfers control to a procedure (can also be called as functions in C language terms), the return address of the call statement is (IP) pushed to the stack.


19.IN instruction:
Input from port into AL or AX register. Second operand is a port number. 


20.OUT instruction:
Output from AL or AX register to port. First operand is a port number.  


21.POP instruction:
Get 16 bit value from the stack and store it in a register. 

Ex. POP DX   ;pops from stack and stores in DX.

22.PUSH instruction:
Store 16 bit value to the stack from a register. 

Ex. PUSH DX   ;pushes value of DX into the stack.
23.XOR instruction:
Logical XOR (Exclusive OR) between all bits of two operands. Result is stored in first operand.
These rules apply: 
  • 1 XOR 1 = 0 
  • 1 XOR 0 = 1
  • 0 XOR 1 = 1
  • 0 XOR 0 = 0

24.XCHG instruction:
Exchange values of two operands.

EX. XCHG AX,DX   ;contents of AX and DX registers are exchanged.



These are some basic instructions i have thought of. If I missed out any thing, let me know by dropping the comments below. :)


Continue to 1A Binary search program


He is a simple passionate tech freak who himself is an engineering student at Canara Engineering college. He likes App Development, Web designing, Blogging, Youtubing, Debugging and also is a CodeGeek!

Sharing is sexy!

Related Articles

4 comments

Write comments
3 March 2017 at 01:15 delete

I was in the hospital and I missed a ton of classes. Your notes are absolutely perfect and saved me. Thanks a lot!

Reply
avatar
Unknown
AUTHOR
5 March 2017 at 02:26 delete

Thanks man :) Glad that it helped you!

Reply
avatar
Anonymous
AUTHOR
30 June 2017 at 15:31 delete

Thanks a lot for sharing this! Helped a lot!

Reply
avatar
18 October 2019 at 22:25 delete

Thank you for sharing Amazing Blog. It's providing very useful guideline for Engineering students.
get more: Microprocessor Programming


Reply
avatar

Share your views about this article!