Reputation: 343
if anyone knows assembler language i'd really need some help debugging my program. I have a 3x3 matrix and a 3 element vector that i read from the console, and i need to multiply them and display the resulting array.
This is my code so far:
name "2x2_matrix"
include "emu8086.inc"
org 100h ; directive make tiny com file.
.model small
.stack 100h
.data
size dw 3
A db ?,?,?,?,?,?,?,?,?
B db ?,?,?
C db ?,?,?
message_a db 10,13,"type the elements of matrix A:$"
message_b db 10,13,"type the elements of array B:$"
rez db 10,13,"the result is:$"
.code
start:
mov ax,@data
mov ds,ax
mov bx, 0 ;
read_a:
;compare with sizexsize
mov ax, size
mul size
cmp bx, ax
je reset_counter
;display message
mov dx, offset message_a
mov ah, 09h
int 21h
;read element
mov ah, 01h
int 21h
sub al, 30h
mov A[bx], al
inc bx
jmp read_a
reset_counter:
mov bx, 0
jmp read_b
read_b:
;compare with size
mov ax, size
cmp bx, ax
je calcul
;display message
mov dx, offset message_b
mov ah, 09h
int 21h
;read element
mov ah, 01h
int 21h
sub al, 30h
mov B[bx], al
inc bx
jmp read_b
calcul:
mov bx,0
mov cx,0
for_i:
mov ax, size
cmp bx, ax
je print
mov al,b.size
mul bx
mov al, A[bx+1]
mov bh, B[bx+1]
mul bh
add C[bx], ah
mov al, b.A[bx+2]
mov bl, b.B[bx+2]
mul al
add C[bx], ah
mov al, b.A[bx+3]
mov bl, b.B[bx+3]
mul al
add C[bx], ah
inc bx
jmp for_i
print:
mov dx, offset rez
mov ah,09h
int 21h
mov ax,size
mul size
mov cx,ax
mov ax,0
mov bx,0
print_c:
cmp bx,cx
je finish
mov al, C[bx]
CALL PRINT_NUM
inc bx
jmp print_c
DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS
finish:
ret
end start
end
It seems to loop and not display anything after i insert the elements, and i'm not sure how to debug it. If anyone is familiar with assembler-type coding and can help me fix my code i'd greatly appreciate it!
Upvotes: 0
Views: 6520
Reputation: 1244
By reading the provided code I found some strange lines:
mov bh, B[bx+1]
mul bh
Here you assign the register bh
(and ultimately bx
too) a new value to be multiplied to ah
. But you also use bx
as the index of your result array. It seems to me as if you change your array index while calculating the necessary information.
The same goes for the following lines
mov al, b.A[bx+2]
mov bl, b.B[bx+2]
mul al
add C[bx], ah
mov al, b.A[bx+3]
mov bl, b.B[bx+3]
mul al
add C[bx], ah
But here you should be aware that you multiply al
with al
which I assume is not the desired operation.
As a solution you could rename every occurence of bh
or bl
to dh/dl
or ch/cl
and change the factor in the multiplication accordingly. Then the index remains the same and the loop will work.
Upvotes: 1