Public Group

# TASM woes: Summing an array of bytes with a word

This topic is 5391 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have an array of bytes that I am summing at the end of each stage of a demo, and I've run into a problem. The function I'm using to output decimal numbers takes a Word as the output. Also, the sum of the Bytes is likely going to end up needing a Word anyway. So, my problem is that I'm finding it difficult to access the array one byte at a time... In a simple console application:
.data
list        db  23h,0FBh,0A4h,76h,65h;

sum         dw  0
.code
main proc
mov ax,@data
mov ds,ax
mov ax,0
mov bx, offset list
mov ax, byte [bx]
inc bx
mov sum,ax


I know it's not looping but I'm just trying to get the first two working. Anyway, with the above source, I'm getting sum as A01E, when it should be adding the first two bytes with a result of 11Eh. I don't want an answer so much as a nudge in the right direction. I need to understand this. Also, does anyone know of any recommendable debuggers for x86? I just started using xp's debug.exe, but I'm not sure that that's the best choice in a large application. I haven't really explored it though either to be honest.

##### Share on other sites
You've got:

mov ax, byte [bx]inc bxadd ax, byte [bx]

I don't know what tasm does with something like that (I would have thought it would complain, since you're trying to read a byte value and store it in a word register). Is it even valid tasm code? I thought tasm requires you to put "byte ptr". Anyway it looks like you really want something like:

mov ah, 0mov al, byte ptr [bx]mov sum, axinc bxmov al, byte ptr [bx]add sum, ax

You might want to consider storing your array as an array of words instead of bytes. That would make it much simpler.

btw I just noticed another post of yours on here from September, asking about "16bit - 32bit conversion". Did you sort that out? Because I can explain some things there if you want..

cheers
sam

##### Share on other sites
I'm definitely working in 16bit for now. I'll have to take you up on that when I move to 32bit (I doubt I'll be using Tasm then). I'll test out your code when I get home in an hour or so.

Thanks a lot.

Edit: Yeah, that does work. I was trying to do it without the intermediate addition to the variable, (mov al, byte ptr[bx] then tried to add to ax the next byte ptr [bx] which gave errors) not that I guess it should matter. Thanks again.

• 21
• 13
• 9
• 17
• 13