Saturday, May 22, 2010

How to average a series of integers using assembly language?

I am trying to write a program that will find the average of a series of integers and store the result in the BX and AX registers.


e.g


Main array = 4, 1, 28, 11, 2, -28, -7


The result will be


(Quotient) BX = 1


(Remainder) AX = 5





This is a pseudo-code for it, but having hard time putting it in assembly language!


I am using visual studio c++


.........................................


First move the numbers in some memory location using mv. Then set one register say CL as the counter and set it to the number of elements in the array. Then start a loop untill the content of CL becomes zero. In each step decrease CL. And read the memory location and add that to some other register say DX. After adding up all the elements divide the sum by the number of elements.


.........................................





Will some one help me out!

How to average a series of integers using assembly language?
Aw come on, replace the words with instructions and it's done for you! Using nasm:





mov dx,0


mov cl, 7


.loopstart


TODO add to values to dx


dec cl


cmp cl, 0


jne. loopstart


(there's a way of doing the dec, cmp and jne all in one go - look it up)


TODO divide the value in dx by 7





There - if you use nasm you only need a header and to fill out two bits of code and it's done. Of course NASM looks a little different to microshaft assembly... It'll take you an hour at the outside to work this out ya lazy little tyke.


No comments:

Post a Comment