8086 program to find sum of odd numbers in a given series
Problem – Write an Assembly Language Program to find sum of odd numbers in a given series containing 8 bit numbers stored in a continuous memory location and store the result in another memory location.
Example –
Example Explanation –
- 500 offset stores the counter value of the series and the elements of the series starts from 501 to 504 offset.
- In this example, we have 4 terms only. Adding the odd terms (found in BL register) 15+07 in AL register and result gets stored (1C) in AL register.
- The result from AL register gets stored in 600 offset.
Assumptions –
- The counter value which tells that how many numbers are there in the series is stored at memory location 500.
- The elements of the series are stored in a continuous memory location starting from 501.
- The result is stored at a memory location 600.
- The starting address of the program is 400.
Test –
Syntax: TEST d, s
It performs AND operation of destination(d) and source(s) but result is not stored only flags ate modified.
Algorithm –
- Load data from offset 500 to register CL.
- Increment the value of offset.
- Load 00H into CH register.
- Load 00H into AL register.
- Load data from offset to register BL.
- Use TEST instruction to check whether data in BL is even or odd, if zero flag is set means data is even then go to step 7 otherwise data is odd then go to step 8.
- Jump to memory location 413H.
- Add the data of AL and BL registers and store the result in AL register.
- Increment the value of offset.
- Jump to memory location 40AH if content of CX is not equal to zero otherwise go to step 11.
- Load the data from AL register to memory location 600.
- End
Program –
Address | Mnemonics | Comments |
---|---|---|
400 | MOV SI, 500 | SI<-500 |
403 | MOV CL, [SI] | CL<-[SI] |
405 | INC SI | SI<-SI+1 |
406 | MOV CH, 00 | CH<-00 |
408 | MOV AL, 00 | AL<-00 |
40A | MOV BL, [SI] | BL<-[SI] |
40C | TEST BL, 01 | BL.01 |
40F | JZ 413 | Jump to 413 memory location if zero flag is set |
411 | ADD AL, BL | AL<-AL+BL |
413 | INC SI | SI<-SI+1 |
414 | LOOP 40A | jump to 40A memory location if content of CX is not equal to zero |
416 | MOV [600], AL | [600], AL |
41A | HLT | end |
Explanation –
- MOV SI, 500 load the value 500 to SI.
- MOV CL, [SI] loads the data of offset SI into CL register.
- INC SI increases the value of SI by one.
- MOV CH, 00 loads the value 00 into CH register.
- MOV AL, 00 loads the value 00 into AL register.
- MOV BL, [SI] loads the data of offset SI into BL register.
- TEST BL, 01 AND operation of content of BL and value 01 and flag registers are modified.
- JZ 413 jump to 413 memory location if zero flag is set.
- ADD AL, BL add the contents of AL and BL registers and store the result in AL register.
- INC SI increases the value of SI by one.
- LOOP 40A jump to 40A memory location if value of CX is not equal to zero.
- MOV [600], AL loads the content of AL register to memory location 600.
- HLT stops the execution of the program.
Contact Us