Tarea de ensamblador: producto escalar de dos matrices

I have a trouble with simple assembler program.I don't know what i why happens but there is problem. Task is : Compute scalar product of two arrays in assembler (emu 8086) Here is my code

    ; multi-segment executable file template.

data segment ; add your data here! n1 db 1,2,3 n2 db 4,5,6 i db ? j db ?

k dw ?
pkey db "press any key...$"

termina

stack segment dw 128 dup(0) ends

code segment start: ; set segment registers: mov ax, data mov ds, ax mov es, ax

mov al,00h
mov ah,02h

mov ch,00h
mov cl,03h

mov bx,offset n1
ciklus:

mov dl,[bx]
;add dl,30h


mov j,0d

add j,dl
int 21h
mov al,j
mov ah,0h


inc bx
inc bx
inc bx
mov dl,[bx]
;add dl,30h

mov i,0d
add i,dl
mul i

mov k,ax


mov al,00h
mov ah,02h
int 21h



dec bx
dec bx
dec bx

inc bx



loop ciklus

lea dx, pkey
mov ah, 9
int 21h        ; output string at ds:dx

; wait for any key....    
mov ah, 1
int 21h

mov ax, 4c00h ; exit to operating system.
int 21h    

termina

end start ; set entry point and stop the assembler.

So when i run program step by step i see strange values in ax. I want 1x4 2x5 3x6 = > 4 10 18 to save in some variable and print to screen .

If someone can help me i will be grateful. P.S this i'm noob in assembler programming so don't be angry of my stupid question.

preguntado el 09 de marzo de 12 a las 22:03

Are you using a debugger to see what goes wrong and when? -

Yes. when i run step by step i see after operation mul i (result should go in ax) and ax is 31h * 34* = 09F4 [2548 dec] , result should be 4 (1*4=4)... i don't know where i'm wrong. -

1 Respuestas

Una vez que el ciklus: label, you are adding 0x30 to dl, luego agregando dl a j, asi que j >= 0x30 at this point. After the interrupt you set al a jy ah a 0x0, asi que ax >= 0x30. Later you add 0x30 a dl, Luego añade dl a i.

So mul i is effectively: [dx:ax] = i * ax, which is going to be at menos (0x30 * 0x30), i.e., ax >= 2304, dx = 0. Larger, depending on the contents of [bx], i, j.

Una vez que el mul sobrescribe el ax register with the value: 0x200 (512), anyway, so the results are lost. In short, you need to look at saving your running ax [ah:al] register before clobbering it for use as the interrupt service code.

PS Podría considerar usar el si y di registers to save working values.

respondido 10 mar '12, 12:03

i know that i need to save result from mul i somewhere but result from mul is 09F4h like i said so i don't need this number... I read your answer but i cant figure why is 09F4 why not 34h ? :( i get lost... will be nice if you can and have time to correct this simple program.btw thanks for answer - user1260255

@user1260255 - I've described why the registers and variables are holding the values they do, and why the mul yields the results you are seeing. You need a pen and paper, some reference material on what the instructions do, what registers they affect, and a step-by-step look at what the debugger is reporting, compared to what you're expecting. It's a short program, and I suspect one of the goals of the assignment is to learn how to debug. - Brett Hale

@user1260255 - I'm not trying to be harsh, but a fragment like: sub dl, 30h; inc bx; inc bx; inc bx; mov dl, [bx]; - why subtract from dl, just to reload it again? Why use inc bx 3 times, when add bx, 3 would suffice? Step through and check the assumptions. What values do i y j hold on the first iteration of the loop? Shouldn't they be initialized to a known value? - Brett Hale

inc bx inc bx inc bx i use cos when i look program it make for me more clearly what i'm doing. In i i store number 4 so when i used mul i i multiple i*ax in ax is j. j=1. So first iteration j=1,i=4 second j=2,i=5 and last j=3,i=6... so they shouldn't be initialized before ciklus.in ciklus they got value what need. And last yes i don't need sub dl,30h :S - user1260255

edit: ooopss sorry i see got wrong value after first ciklus in i and j ...probably need to reset i and j :)) - user1260255

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.