Reputation: 21
I'm very new to assembly 8086. I need to write a program which copy only the positive numbers in address 0-10h to a memory block which start at 20h and save the amount of positive numbers in dh.
I thought that the best way will be to make an array and copy it to a second array at 20h, but I don't know how to make arr2 to start at 20h, I also tried to make a value "size" which inc each time the loop is performing.
This is my code so far:
org 0h
.DATA
arr1 DB 0h,-1h,2h,3h,-4h,5h,6h,-7h,8h,9h,10h
arr2 DB 11 dup(?)
size DB 1h
.CODE
main:
mov ax,@DATA
mov si,0
copyloop:
mov al, arr1[si]
mov arr2[si], al
inc size
inc si
cmp si, 9
jne copyloop
move dh,size
ret
Upvotes: 2
Views: 2602
Reputation: 9899
but I don't know how to make arr2 to start at 20h
One way to make your second array start at address 20h, is to fill the gap between the end of the first array and the start of the second array.
The calculation looks like:
db (TargetAddress - CurrentAddress) dup 0
The target address of course is 20h, the current address is given by the special symbol $.
db 20h-$ dup (0)
save the amount of positive numbers in dh.
If you're only interested in the positive numbers, then it doesn't help to inc size
on every iteration of the loop. Just increment the counter if the value that you copy to the second array is positive.
Also, why did you initialize the size variable to one? Starting it at zero makes more sense.
I need to write a program which copy only the positive numbers in address 0-10h
.DATA
arr1 db 0h, -1h, 2h, 3h, -4h, 5h, 6h, -7h, 8h, 9h, 10h
db 20h-$ dup (0)
arr2 db 11 dup (?)
size db 0
.CODE
main:
mov ax, @DATA
mov ds, ax ;You forgot this instruction!
xor bx, bx
xor si, si
copyloop:
mov al, arr1[si]
test al, al
js NotInterested ;Skip negative number
mov arr2[bx], al ;Copy positive number
inc bx
inc size
NotInterested:
inc si
cmp si, 11 ;Repeat for the 11 elements in first array
jb copyloop
mov dh, size ;Result to DH
mov ax, 4C00h ;Preferred way to end program on emu8086
int 21h
This solution can easily do without the size variable.
The count is already available from the value in BX
that was used to index the second array.
.DATA
arr1 db 0h, -1h, 2h, 3h, -4h, 5h, 6h, -7h, 8h, 9h, 10h
db 20h-$ dup (0)
arr2 db 11 dup (?)
.CODE
main:
mov ax, @DATA
mov ds, ax ;You forgot this instruction!
xor bx, bx
xor si, si
copyloop:
mov al, arr1[si]
test al, al
js NotInterested ;Skip negative number
mov arr2[bx], al ;Copy positive number
inc bx
NotInterested:
inc si
cmp si, 11 ;Repeat for the 11 elements in first array
jb copyloop
mov dh, bl ;Result to DH
mov ax, 4C00h ;Preferred way to end program on emu8086
int 21h
Upvotes: 1
Reputation: 29052
Because you were copying/moving array entries, you can make good use of the LODSB instruction and STOSB instruction after the compare.
JL
jumps if the comparison evals to 'LESS THAN ZERO'.
org 0h
.DATA
; here the position is 0h
arr1 DB 0h,-1h,2h,3h,-4h,5h,6h,-7h,8h,9h,10h
org 20h ; set position to 20h
; here we are at position 20h
arr2 DB 11 dup(?)
size DB 1h
.CODE
main:
mov ax,@DATA
mov ds, ax ; set data segment
lea si,arr1 ; address of source array (at 0h)
lea di,arr2 ; address of destination array (at 20h)
copyloop:
lodsb ; load number in si and inc si
cmp al, 0 ; check if number is positive
jl copyloop ; jump next if AL is less than zero
stosb ; store positive number in [di] and inc di
cmp si, 10 ; check if maximum length of 9 is reached
jbe copyloop ; if SI is below or equal to 9, continue loop
mov dh, size ; unknown function (!!!) - you didn't address this function
ret
Upvotes: 1