2

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
2
  • 1
    If your program starts at ORG 0h, then address 20h is ... 20h bytes after that. Use padding or code to fill the gap. Commented Jan 10, 2018 at 21:35
  • can you give an example? thnx Commented Jan 10, 2018 at 21:49

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

5 Comments

thnx! I still need more work on it but it sure helped a lot :)
Note that only MASM-style assemblers (like emu8086) can use ORG to introduce padding in the output (by skipping a range of bytes). NASM doesn't work that way: nasm.us/doc/nasmdo12.html#section-12.1.3 shows how to use TIMES 510-($-$$) DB 0 to calculate how much padding is needed to reach a given position relative to the start of a section.
The array has 11 elements. Therefore the cmp si,9 is 1 short to process the last element.
@SepRoland: thx, fixed that. I just copied it from the question without further ado. My bad.
Since you're not checking the array index if the element is negative, the code would fail if the last element were negative!
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.