I have this code ( having a string of length n, build another of length n-2 as it follows: sir2[i]=(sir[i]+sir[i+1]+sir[1+2])/3
) and I can't figure out why it is freezing. there are no errors and I've been debugging for a while now, but I can't figure out if the problem is with the algorithm.
prints macro number
local decompose, pops
mov bx,10
mov al, number
mov cx,0
decompose: ;pushing digits to the stack
inc cx
mov ah,0
div bl
mov dl,ah ; remainder - last digit
add dx,48 ; to transform it in its char version
push dx
cmp al,0
jnz decompose
pops: ; pop digits off the stack
pop dx
mov ah,2h
int 21h
loop pops
; pretty spacing
mov dl,' '
mov ah,2h
int 21h
endm
data segment para public 'data'
sir db 5, 10, 12, 4, 3
n equ $-sir
sir2 db n-2 dup(0)
data ends
code segment para public 'code'
start proc far
assume cs:code,ds:data
push ds
xor ax,ax
push ax
mov ax,data
mov ds,ax
mov si,0
mov dx,3
mov cx,n
sub cx,2
l1:
mov ah,0
mov al,sir[si]
add al,sir[si+1]
add al,sir[si+2] ;sum of 3 numbers
div dx ;sir2[i]=(sir[i]+sir[i+1]+sir[1+2])/3
mov sir2[si],al
inc si
cmp si,cx
jbe l1
mov si,0
l2:
prints sir2[si]
inc si
loop l2
ret
start endp
code ends
end start
Can anyone help?
Change
div dx ;sir2[i]=(sir[i]+sir[i+1]+sir[1+2])/3
to
div dl ;sir2[i]=(sir[i]+sir[i+1]+sir[1+2])/3
(see Michael's answer).
A macro is a piece of code which is inserted "as is" at the place where the macro is called. The macro prints
changes CX
which you need untouched for loop l2
. Rewrite the loop:
mov si, 0
mov di, cx
l2:
prints sir2[si]
inc si
dec di
jnz l2
BTW: Don't forget to define a stack:
_STACK SEGMENT PARA STACK 'STACK'
dw 1024 dup (?)
_STACK ENDS