Copy a pixel from coords (x0,y0) to (x1,y1)

147 views Asked by At

I'm trying to copy a bunch of pixels from a screen location into other location. Picture this, I have a 8x8 green square on coordinates (100,120), and I want to copy the square to coordinates (150,60).

I'm using graphic mode 13h. That means 320x200, so my square begins in address 38500 (using y*320+x).

DS points to 0A0000h.

How can I copy this square to the other coords (19350)?

I tried something like this:

  MOV SI,38500
  MOV DI,19350
INIT:
  MOV CX,4          ; loop 4 times
COPY_BOX:
  MOV AX,DS:[SI]    ; copy two pixels
  MOV DS:[DI],AX
  ADD SI,2          ; move to the next two pixels
  ADD DI,2
  LOOP COPY_BOX
  ADD SI,312        ; drop one line and position on the first pixel
  ADD DI,312
  JMP INIT          ; copy next row of pixels

; do this for the 8 rows

What am I doing wrong?

1

There are 1 answers

0
Sep Roland On BEST ANSWER
JMP INIT          ; copy next row of pixels

This is where your program goes into an infinite loop.
You only need to repeat the code height=8 times.
I solved it by putting these small counters in CH and CL:

  MOV SI,100+120*320 ;(100,120)
  MOV DI,150+60*320  ;(150,60)
  MOV CH,8           ; loop 8 times vertically
INIT:
  MOV CL,4           ; loop 4 times horizontally
COPY_BOX:
  MOV AX,DS:[SI]     ; copy two pixels
  MOV DS:[DI],AX
  ADD SI,2           ; move to the next two pixels
  ADD DI,2
  DEC CL
  JNZ COPY_BOX
  ADD SI,320-8       ; drop one line and position on the first pixel
  ADD DI,320-8
  DEC CH
  JNZ INIT   

If you're willing to use the string primitives it could look like this :

  CLD
  PUSH DS             ; 0A000h
  POP  ES
  MOV  SI,100+120*320 ;(100,120)
  MOV  DI,150+60*320  ;(150,60)
  MOV  AX,8           ; loop 8 times vertically
INIT:
  MOV  CX,4           ; 4 x 2 pixels
  REP MOVSW           ; copy 1 line of pixels
  ADD  SI,320-8       ; drop one line and position on the first pixel
  ADD  DI,320-8
  DEC  AX
  JNZ  INIT