Find the average of two values, Computer Engineering

Assignment Help:

Q. Find the average of two values?

Find the average of two values which are stored in 

; Memory locations named FIRST and SECOND 

; And puts result in memory location AVGE.

; Input:  Two memory variables stored in memory locations FIRST and SECOND

; REGISTERS            ; Uses DS, CS, AX, BL

; PORTS                      ; None used

DATA             SEGMENT

FIRST             DB   90h         ; FIRST number,                      90h is a sample value

SECOND        DB   78h         ; SECOND number,                 78h is a sample value

AVGE             DB   ?              ; Store average here

DATA ENDS

CODE   SEGMENT

                        ASSUME CS:CODE, DS: DATA

START:           MOV    AX, DATA               ; Initialise data segment, i.e. set

                        MOV    DS, AX                     ; Register DS to point to Data Segment

                        MOV    AL, FIRST                            ; Get first number

                        ADD    AL, SECOND           ; add second to it 

                        MOV    AH, 00h                     ; Clear all of AH register

                        ADC    AH, 00h                     ; Put carry in LSB of AH

                        MOV    BL, 02h                     ; Load divisor in BL register

                        DIV      BL                             ; Divide AX by BL. Quotient in AL,

                                                                        ; And remainder in AH

                        MOV    AVGE, AL                ; Copy result to memory

CODE ENDS

END START

Discussion: 

An add instruction can't add two memory locations directly so we moved a single value in AL first and added the second value to it.

Please consider on adding the two values there is a possibility of carry bit. (The values here are being considered as unsigned binary numbers). Now problem is how to putcarry bit in the AH register in such a way that AX(AH:AL) reflects added value. This is done employing ADC instruction.

ADC AH,00h instruction would add immediate number 00h to contents of carry flag and contents of AH register. The result will be left in AH register. As we had cleared AH to all zeros before the add we actually are adding 00h + 00h + CF. Result of all this is the carry flag bit is put in AH register that was anticipated by us.

Lastly to get the average we divide the sum given in AX by 2. A more universal program would need negative and positive numbers. After division 8-bit quotient would be left in AL register that can then be copied in the memory location called as AVGE.


Related Discussions:- Find the average of two values

Write the program to use arrays, In class SlotCollection, remove the fields...

In class SlotCollection, remove the fields slot1, slot2, slot3 and slot4 and replace them by an array of slots. Modify the rest of the program to work using this array. There is

Working of fully parallel associative processor, Q. Working of Fully Parall...

Q. Working of Fully Parallel Associative Processor? Fully Parallel Associative Processor: This processor accepts the bit parallel memory organisation. There are 2 kinds of this

Design a circuit which computes the square of a number, Design a circuit wh...

Design a circuit which computes the square of a number? This should not make use of any multiplier circuits. This should use Multiplexers and some other logic as: 1^2=0+1=1

Explain high performance of instruction execution, Q. Explain High performa...

Q. Explain High performance of Instruction execution? High performance of Instruction execution: While mapping of HLL to machine instruction the compiler favours relatively sim

Network topology, According to the report, network 1 and network 2 are not ...

According to the report, network 1 and network 2 are not able to reach network 3. As shown on OTBNetwork Topology above, OTB Inc. has 2 routing protocols running due transition iss

Integrating virtual memory, Integrating Virtual Memory, TLBs, and Caches - ...

Integrating Virtual Memory, TLBs, and Caches - computer architecture:   There are 3 types of misses: 1. a cache miss 2. TLB miss 3. a page fault 2 techniqu

Define the time required by sector to reach below read/write, is the time r...

is the time required by a sector to reach below read/write head. Latency Time is the time needed by a sector to reach below read/write head.

How can we minimize problems occurring because of branch., Q. How can we mi...

Q. How can we minimize problems occurring because of the branch instructions? We may use various mechanisms which may minimize the effect of branch penalty. To keep mult

Define various system, Define various system? Single job system: Only...

Define various system? Single job system: Only one program may be run at a time, and therefore only one person might be work on a machine at one time.  Multi job system:

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd