Search This Blog

Thursday, April 26, 2012

Program for factorial of a number

MY_DATA SEGMENT
    NUMBER DW STORE_NUMBER_HERE
    MY_FACT DW ??
MY_DATA ENDS

CODE SEGMENT
    ASSUME CS:CODE, DS:DATA
    ; INITLISE DATA SEGMENT
    MOV AX, MY_DATA
    MOV DS, AX
   
    MOV AX, 1
    ; INITILISE AX WITH 1
    MOV BX, NUMBER
   
BACK:    MUL BX
    DEC BX
    JNZ BACK
    MOV MY_FACT, AX
    ; STORE ANSWER IN MY_FACT
    HLT
; PROGRAM ENDS
CODE ENDS
END   

Program to sort numbers in ascending order

; for that assume there are N words to be arranged

MY_DATA SEGMENT
    NUM_ARRAY DW 0100H, 0098H, 0012H, 0099H
    ; THE ARRAY OF N NUMBERS
    ARRAY_SIZE DW 0004H
    ; SIZE OF THE ARRAY
MY_DATA ENDS

CODE SEGMENT
    ASSUME CS:CODE, DS:MY_DATA
    ; THE TWO INITIALISATION STATEMENTS
    MOV AX, MY_DATA
    MOV DS, AX
   
    MOV DX, 2
    ; FOR SECONDS NUMBER THAT WILL GO ON INCREMENTING
   
FIRST:    MOV CX, DX
    DEC CX           
    ; THE FIRST NUMBER TO BE COMPARED WITH
    MOV SI, CX         ; STORE THIS IN SI
    ADD SI, SI

    MOV AX, NUM_ARRAY[SI]     ; GET THE NUMBER
   
BACK:    CMP NUM_ARRAY[SI-2], AX ; COMPARE THE 2 NUMBERS
    JBE NEXT        ; JUMP BELOW OR EQUAL
    MOV DI, NUM[SI-2]
    MOV NUM[SI], DI   
    ; PUT THE NUMBER IN THE CORRECT PLACE
    DEC SI
    DEC SI
    DEC CX
    JNZ BACK;
NEXT:     MOV NUM[SI], AX        ; PUT AX IN CORRECT POSITION
    INC DX
    CMP DX, ARRAY_SIZE    ; TO BE DONE FOR ALL NUMBERS IN AN ARRAY
    JBE FIRST        ; JUMP BELOW AND EQUAL FIRST
    HLT
    ; ARRAY SORTED IIN ASCENDING ORDER
; NOW SORT SAME ARRAY IN DESCENDING ORDER
    MOV CX, 2        ;{ARRAY_SIZE/2}
    MOV DX, ARRRAY_SIZE    ; INITIALISE DX
    MOV SI, 0000H        ; INITIALISE SI
; THE DESCEND BLOCK WILL JUST MOVE THE ARRANGED ARRAY IN REVERSE ORDER
DESCEND:MOV AX, NUM_ARRAY[DX]
    MOV BX, NUM_ARRAY[SI]
    MOV NUM_ARRAY[DX], BX
    MOV NUM_ARRAY[SI], AX
    INC SI
    DEC DX
    DEC CX
    JNZ DESCEND            HLT
; PROGRAM ENDS
CODE ENDS
END

Understanding the program structure of 8086 microprocessor

The program writing in 8086 is quite different from that of 8085. Why one may ask?? Well answer to that is first their architecture are different. Different architecture means different set of instruction code. Secondly 8086 is a 16 bit processor. Meaning it processes 16bit of data, twice as much data as the 8085.

There are 2 ways to write a program. One as I say a simplified segment way other the simple way.

Simplified Segment
Simple way
Basic Structure
Basic Structure
.model model_type
.data data_segment_name
-------
.code code_segment_name
----
----
----
END
Data_segment_name SEGMENT
----
----
Data_segment_name ENDS
Code_segment_name SEGMENT
----
----
Code_segment_name ENDS
Here we don’t need to end each and every segment by using ENDS instruction. When compiler detects start of another segment or meets the instruction END it automatically assumes the end of the segment.
Here every segment needs to ended properly using Segment ENDS instruction. Failing to do so will raise a syntax error.
Here writing name of data segment or code segment is not nesscessary. A simple .code and .data will create a local reference type segment usable in the single file.
Here writing the name of data segment and the code segment is very very essential. The segment created here needs to have a name without which complier will not be able to differentiate between memory segmentation. No two segments can have a same name.
Here model type is to written as small, huge, large etc etc…
Here model type needn’t to be mentioned.

Rest explanation of program code can be found as comment along with the program.

Friday, April 13, 2012

Intel 8087 Internal Architecture

Intel 8087 Internal Architecture
The 8087 is divided into 2 sections CU and NEU. That’s is control unit and numeric execution unit.
The numeric execution unit executes all numeric processor instructions while control unit receives, decodes instructions, read and writes memory operands and executes 8087 control instructions.

These 2 units works asynchronously with each other. The control unit is majorly responsible for establishing communication between CPU and memory and also for coordinating internal coprocessor execution.

The NEU has 8 registers 80 bit wide stack that holds operands for arithmetic instructions and their result. The FSTSW AX; is the instructions used for communication between coprocessor and micro processor.

The control unit is used to synchronize operation between coprocessor and microprocessor.  The unit has a control word a status word and a data buffer. If the instruction is an ESCape (coprocessor) instruction the coprocessor executes it otherwise the microprocessor executes it. The status register reflects the overall operation of the coprocessor.

The 8087 is a numeric data processor. It is basically made to work along with the 8086 and 8088 processors. It is incapable of fetching the instructions on its own so it is just simply connected to respective buses of the processor. Its instructions are recognized by word F as each and every instructions starts with F. for example FADD, FSUB etc.

The BUSY pin of the coprocessor is connected to the main processor's TEST pin. This TEST pin is active low in nature. This is because the speed of operation of the coprocessor and the main processor is different.


Various memory models

Memory model
Description
TINY
Has only one segment. Therefore both data and code combined cannot be greater than 64k.
SMALL
Has one code segment and one data segment. Therefore each code and data cannot be greater than 64k.
MEDIUM
Has more than one code segment and has only one data segment. So code can be greater than 64k but data cannot be greater than 64k.
COMPACT
Has one code segment and many data segment. Hence data can be greater than 64k but code cannot be greater than 64k.
LARGE
Has more than one code segment and more than one data segment. No array is larger than 64k. thus both data and code can be greater than 64k.
HUGE
More than one code segment and more than one data segment. Arrays may be larger than 64k. hence both data and code can be greater than 64k.
FLAT
Has only one segment upto 4GB. All data and code including system resources are in a single 32 bit segment.

Simplified Segment Directives

The simplified segment directives are .CODE, .DATA, .MODEL, .STACK etc.
The .CODE directive may be followed by a name or a label for the segment.
The .STACK directive may be followed by the size of the stack segment. Default is 1k.
The definition of the simplified segment extends from a simplified segment directive to another simplified segment directive or up to END directive if the defined segment is the last one.
For example…
.MODEL SMALL
.DATA
A DW 1234H
B DW 0124H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AX,A
MOV BX,B
SUB AX,BX
MOV AH,4CH
INT 21H
END