Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Help Please!

Status
Not open for further replies.

Cuso

MIS
Jul 11, 2005
8
US
I need to create an array of 20 values (bytes) and compute the average. I'm new in this. This is my code for the addition phase:

.MODEL SMALL
.STACK 100h
.DATA
arreglo db 3, 8, 5, 3, 9, 2, 1, 4, 5, 11, 2, 1, 12, 10, 7, 17, 18, 6, 14, 13
arrsuma db ?
promedio db ?

.CODE
main proc

mov ax,@DATA
mov ds,ax
mov al,arreglo[0]
add al,arreglo[1]
add al,arreglo[2]
add al,arreglo[3]
add al,arreglo[4]
add al,arreglo[5]
add al,arreglo[6]
add al,arreglo[7]
add al,arreglo[8]
add al,arreglo[9]
add al,arreglo[10]
add al,arreglo[11]
add al,arreglo[12]
add al,arreglo[13]
add al,arreglo[14]
add al,arreglo[15]
add al,arreglo[16]
add al,arreglo[17]
add al,arreglo[18]
add al,arreglo[19]
mov arrsuma,al
mov ax,4c00h
int 21h


main endp
end main

--------end of code------------

the problem is that when I create the executable it only have 1kb and when I use debug to see if it is doing what I want the it doesn't do it. Help me please

 
Problem 1: Array Indexing in assembler is done as follows:
Code:
; load base offset in an base register
mov bx,offset arreglo
; get first item in array
mov ax, byte [bx]
; now add second item
add ax,byte [bx+1]
while there are some assemblers that utilize the base[index] form of accessing arrays, it is because they use a macro to translate to the actual assembler instructions.

problem 2:
your assumption that the end result is less than 255, and therefore will fit into a byte sized register. While in this particular case it is, what happens when the result is greater than 255, the overflow flag is set and the result "goes around the corner."

Problem 3: Most assemblers default to hex radix, meaning that indexes and integers without a radix designator are assumed to be in hexadecimal format. Your assumption is indexes are decimal. As a safety measure ALWAYS designate numerals with the desired radix, i.e. 3fH, 21d 56o, 1011b. Saves a lot of hair loss. :)

The simplest solution is the best!
 
Assemblers build your executables ONLY with the code YOU specify. HLL compilers mostly add
a lot of additional code (debug info, error checking, memory management etc.) to your final
program, stuff you might not even use or need. That's why assembler programs are so small.

what is exactly going wrong in your debug session?

if you want, you can use the following code instead of all the "add al,xxx" lines. it is shorter.

Code:
  mov si,offset arreglo
  mov cx,sizeof arreglo
  xor ah,ah

lp:
  lodsb
  add ah,al
  loop lp

  mov arrsuma,ah
 
Thank You for your time and your help. I used the tips that denc4 recommend and worked. Thanks Alot!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top