finding a consecutive sequence of zeros assembly language programming
; it is code for finding sequence of 0's
[org 0x0100]
jmp start
findConsecutiveZeros: ; it is start of subroutine
push bp
mov bp, sp
push ax
push bx
push cx
push dx
push si
push di
mov si, [bp+4]
mov dx, 0
mov bx, 31 ; it a pointer to access data from memory
l4: mov al, [data+bx]
mov cx, 8 ; it is counter for shifting loop
l1: shr al, 1
jc l2 ; it jumps if carry flag is 1
add di, 1
cmp di, si
je l3
sub cx, 1
jz l5
jmp l1
l2: mov di, 0
add dx, 1 ; it find our index of first zero
sub cx, 1
jnz l1
l5: sub bx, 1
cmp bx, 0xffff
jne l4
mov word[bp+6], -1
jmp end1
l3: mov word[bp+6], dx
end1:
pop di
pop si
pop dx
pop cx
pop bx
pop ax
pop bp
ret 2 ; it is end of subroutine
start : ; it is start of main
sub sp, 2
push 14 ; it is set by user (number of 0's to find)
call findConsecutiveZeros
pop ax ; if 0's found then this register contains starting
; index number otherwise " -1 "
mov ax, 0x4c00
int 0x21 ; it is end of main
data : db 128,1,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
[org 0x0100]
jmp start
findConsecutiveZeros: ; it is start of subroutine
push bp
mov bp, sp
push ax
push bx
push cx
push dx
push si
push di
mov si, [bp+4]
mov dx, 0
mov bx, 31 ; it a pointer to access data from memory
l4: mov al, [data+bx]
mov cx, 8 ; it is counter for shifting loop
l1: shr al, 1
jc l2 ; it jumps if carry flag is 1
add di, 1
cmp di, si
je l3
sub cx, 1
jz l5
jmp l1
l2: mov di, 0
add dx, 1 ; it find our index of first zero
sub cx, 1
jnz l1
l5: sub bx, 1
cmp bx, 0xffff
jne l4
mov word[bp+6], -1
jmp end1
l3: mov word[bp+6], dx
end1:
pop di
pop si
pop dx
pop cx
pop bx
pop ax
pop bp
ret 2 ; it is end of subroutine
start : ; it is start of main
sub sp, 2
push 14 ; it is set by user (number of 0's to find)
call findConsecutiveZeros
pop ax ; if 0's found then this register contains starting
; index number otherwise " -1 "
mov ax, 0x4c00
int 0x21 ; it is end of main
data : db 128,1,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
Comments
Post a Comment