Search This Blog

Showing posts with label Microprocessor 8086/8088 and interfacing. Show all posts
Showing posts with label Microprocessor 8086/8088 and interfacing. Show all posts

Tuesday, June 12, 2012

Program to determine whether the number is a bit wise palindrone or not

MY_DATA SEGMENT
    X DW MY_NUMBER
    MSG1 DB 'NUMBER IS A PALINDRONE'
    MSG2 DB 'NUMBER IS NOT A PALINDRONE'
MY_DATA ENDS
CODE SEGMENT

    ASSUME CS:CODE, DS:MY_DATA

    START:
    MOV AX, MY_DATA
    MOV DS, AX
    ; THE AOVE 2 STTEMNETS INITILISE THE DATA SEGMENT

    MOV CL, 10H ; AS DOUBLE WORD IS 16 BITS
   
    UP:
    ROR AX, 1
    ; ROTATE RIGHT ONE TIME
    RCL DX, 1
    ; ROTATE LEFT WITH CARRY ONE TIME
   
    LOOP UP
    ; TILL COUNTER GOES TO 0
   
    CMP AX, DX
    JNZ NOPALIN
    ; NO ZERO MEANS NOT A PALINDRONE SO DECLARE THAT
   
    LEA DX, MSG1
    ; DECLARE AS PALINDRONE IF ZERO
   
    ; IF USING TASM WRITE THESE TWO LINES
    MOV AH, 09H
    INT 21H
   
    JMP EXIT
   
    NOPALIN:
    LEA DX, MSG2
    ; DECLARE NOT A PALINDRONE
    ; IF USING TASM WRITE BELOW TWO LINES
    MOV AH, 09H
    INT 21H

    EXIT:
    ; IF USING TASM WRITE THESE TWO LINES TO END
    MOV AH, 4CH
    INT 21H

CODE ENDS
END

Program to find GCD of two numbers


MY_DATA SEGMENT
                NUM1 DW 000AH
                NUM2 DW 0004H
                GCD DW ?
MY_DATA ENDS

CODE SEGMENT
                ASSUME CS:CODE, DS:DATA
                START:
                MOV AX, DATA
                MOV DS, AX
                ; INITIALISATION OF DATA SEGMENT
               
                MOV AX, NUM1
                ; MOVE FIRST NUMBER TO AX
                MOV BX, NUM2
                ; MOVE SECOND NUMBER TO BX
               
                UP:
                CMP AX, BX
                JE EXIT
                ; IF EQUAL THEN NO POINT IN FINDING GCD
                JB EXCG
                ; IF FIRST NUMBER IS SMALLER PUT IT BX
                ; MEANING SMALL NUMBER MUST STAY IN BX
               
                UP1:
                MOV DX, 0000H
                ; INITIALISE DX
                DIV BX
                ; DIVIDE (BIGGER NUMBER)/(SMALLER NUMBER)
                CMP DX, 0
                ; SEE IF REMAINDER IS ZERO OR NOT
                JE EXIT
                ; IF ZERO VOILA YOU HAVE FOUND THE GCD IN THE SMALLER NUMBER
                MOV AX, DX
                ; IF NON ZERO MOVE REMAINDER TO AX
                JMP UP

                EXCG:
                XCHG AX, BX
                ; EXCHANGE CONTENTS OF AX AND BX
                JMP UP1
               
                EXIT:
                MOV GCD, BX
                ; STORE RESULT IN GCD
               
                ; WRITE THESE TWO LINES IF YOU ARE TESTING IN TASM
                MOV AH, 4CH
                INT 21H
                ; THE ABOVE TWO LINES ARE OBVIOUSLY THE EXIT INTERRUPT
                ; USED IN TASM

CODE ENDS
END START
Understanding the program
What I did was basically divide the larger number with the smaller number.
If we get remainder 0 then the smaller number is our GCD for the two given number.
If we don’t get a zero remainder we transfer the remainder to AX and then again repeat the process till we get that zero remainder. In short I perform the same procedure we use mathematically to find the Greatest Common Denominator.

Friday, May 25, 2012

8086 Memory Banks

The 8086 has 20-bit address bus, so it can address 2^20 or 1,048,576 addresses. Each address represents a stored byte. To make it possible to read or write a word with one machine cycle, the memory for an 8086 is set up in to 2 banks of up to 524,288 bytes each. See figure.
8086 Memory Banks


One memory bank contains all the bytes which have even addresses such as 00000h, 00002h, and 00004h etc. the data lines of this bank is connected to the lower 8 bit data lines i.e. from D0 to D7 of 8086.

The other memory bank contains all bytes which have odd addresses such as 00001h, 00003h and 00005h etc. the data lines of this bank is connected to the upper 8 bit data lines i.e. from D8 to D15 of 8086.

Address line A0 is used for enabling the memory device in the lower bank. An addressed memory device in this bank will be enabled when A0 is low, as it will be for any even address.
Like address 00222h = 0000 0000 0010 0010 0010.
Address lines A1 to A19 are used to select the desired memory device in the bank and hence the desired byte in the device.

Address line A1 to A19 are also used to select the desired memory device in the upper bank and hence the desired byte. An additional part of enabling the upper bank memory device is handled by the BHE i.e. the bus high enable signal. This is multiplexed out from the 8086 at the same time as an address is sent out.  An external latch, strobbeed by the ALE signal, grabs the BHE signal and holds it stable for the rest of the machine cycle like it does for the address.

So now if we read a byte from or write a byte to an even address the A0 will be low BHE will be high enabling the lower bank and disabling the upper bank.

The main reason that the A0 and BHE signal work as they do is to prevent the writing of an unwanted signal.

Friday, May 18, 2012

BIOS Routine (in short)


  •   The ROM BIOS (Basic Input Output System) is part of the ROM based control system of an IBM PC or compatible that both defines the architecture of the computer to the software.
  •   provides the fundamental I/O services that are needed for the operation of the computer.
  •   The BIOS is actually a collection of procedures.
  •   Each procedure performs a specific function such as reading a character from the keyboard, writing characters to the screen, or reading information from disk.
  •   To call one of these procedures, load required parameters in specified registers and execute an INT N instruction. N in this case is the interrupt type which vectors to the desired type.

Interrupts in 8086

The meaning of ‘interrupts’ is to break the sequence of operation. While the cpu is executing a program, on ‘interrupt’ breaks the normal sequence of execution of instructions, diverts its execution to some other program called Interrupt Service Routine (ISR).
After executing ISR , the control is transferred back again to the main program.
CPU checks for interrupts after every step.

Purpose of Interrupts
Interrupts are very helpful in data input output. CPU uses interrupts to communicate with I/o devices that provide data at a relatively low transfer rate.

Interrupts Types

They are divided into the group of 3.
Type 0 – Type 4: these perform fixed operation so are called dedicated interrupts.
Type 5 – type 31 are reserved for higher processors like 80386 etc etc.
Type 32 – type 255 are user defined interrupts. These can be hardware or software or can be acticvated by INTR line.

They are namely

Interrupt type
Description
Type 0 – divide error
8086 performs a complicated instruction like IDIV and DIV. so for connivance 8086 automatically does a divide by zero interrupt if the result of DIV and IDIV operation is too large to fit in the destination register or the divisor is 0.
For type 0 interrupt, the 8086 pushes the flag register on the stack, resets IF and TF and pushes return address(CS and IP) on the stack.
As this is automatic and cannot be disabled in any way, then you have to account for it whenever one uses IDIV and DIV. one way is to make sure results ever become larger than the register. Another way is to simply write an ISP which takes the desired action.
Type 1 – single step
When we tell a system to single step. It will execute 1 instruction then it will stop. We can then examine the contents of the registers and the memory. Basically it’s a debugging of our program. If we are statisfied we can tell the system to move on to the next instruction.
In this system waits for our direction. The trap flag makes it easy to implement a single step feature.
When trap flag is set =, it automatically does a type 1 interrupt. It pushes flag register on the stack, resets TF and IF and pushes CS and IP values for the next instruction on the stack.
Type 2 – non maskable
8086 automatically does a type 2 interrupt when it receives a low to high transition on its NMI pin. Again when acknowledged, it pushes flag register on the stack, resets TF and IF and pushes CS and IP values for the next instruction on the stack.
The name nonmaskable given means that it cannot be disabled by any program instruction. If activated must be acknowledged by the processor. As this pin cannot be activated accidentally so we use it to signal the 8086 that some external condition needs to be taken care of.
Type 3 – breakpoint interrupt
This interrupt is produced by execution of INT 3 instruction. The main use is to implement a breakpoint function in a system.
When we insert a breakpoint, the system executes all the instructions upto the inserted breakpoint then stops. CCh is the code for INT 3 instructions.
We can use it to check our program execution in blocks. Or in simple system we can give control back to user. Or in a complicated system all execution summary can be displayed on a screen.
Type 4 – overflow
The overflow flag will be set if the signed result of an arithmetic operation on two signed numbers is too large to be represented in the destination register or the memory.
There are 2 major ways to detect and respond to the overflow. One way is to put a jump instruction i.e jump on overflow right after the arithmetic operation so that it will jump to an error routine to show that overflow error has occurred.
The second way of detecting is to put an interrupt on the overflow instruction, INTO immediately after arithmetic operation. If OF is not set it will work as NOP, but if its set it will give type 4 interrupt after it does INTO instruction.

Interrupt priority
Interrupt
Priority
DIVIDE ERROR, INTO, INT n
Highest
NMI

INTR

SINGLE STEP
Lowest