News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

code help

Started by xxxx, February 03, 2005, 12:55:15 PM

Previous topic - Next topic

xxxx

the following code that i write is suppose to copy a set of data from one location to another.plz help me identify what went wrong.
thanks.



.MODEL SMALL
.STACK 64
.DATA
ORG 10H
DATA_IN DB 41H,42H,43H,44H,45H,46H


ORG 20H
COPY     DB  6 DUP(?)
   


.CODE

MAIN PROC FAR

    MOV AX,@DATA
    MOV DS,AX
    MOV SI,OFFSET DATA_IN
    MOV CX,06H
   
   
    AGAIN: MOV AL,[SI]
           MOV COPY,AL
           INC SI
           INC COPY
           LOOP AGAIN
   
   
   
   
   
   
   
   

MOV AH,4CH
INT 21H

     MAIN ENDP
     END MAIN

UncannyDude

You cannot "INC COPY", as copy is a (read-only) label.

    MOV AX,@DATA
    MOV DS,AX
    MOV DI,OFFSET COPY
    MOV SI,OFFSET DATA_IN
    MOV CX,06H
   
   
    AGAIN: MOV AL,[SI]
           MOV [DI],AL
           INC SI
           INC DI
           LOOP AGAIN


The first 4 instructions in AGAIN does a MOVSB, and the last one is a REP

xxxx

You cannot "INC COPY", as copy is a (read-only) label.

but you can do COPY+1,COPY+2,...,COPY+n right?

are there any other methods to accomplish the same task?

thanks

MichaelW

xxxx,

COPY is a data label that represents the address of the first byte following the label. INC COPY will increment the value of the first byte at the address COPY.

When you use COPY as an operand, because COPY represents a constant, you can specify a displacement that MASM will add to the address when it encodes the instruction. You could use this capability to perform your copy operation as:

    MOV AL,DATA_IN
    MOV COPY,AL
    MOV AL,DATA_IN+1
    MOV COPY+1,AL
    MOV AL,DATA_IN+2
    MOV COPY+2,AL
    MOV AL,DATA_IN+3
    MOV COPY+3,AL
    MOV AL,DATA_IN+4
    MOV COPY+4,AL
    MOV AL,DATA_IN+5
    MOV COPY+5,AL

For which MASM would generate these instructions:

0A38:000B A01C00        MOV     AL,[001C]
0A38:000E A22C00        MOV     [002C],AL
0A38:0011 A01D00        MOV     AL,[001D]
0A38:0014 A22D00        MOV     [002D],AL
0A38:0017 A01E00        MOV     AL,[001E]
0A38:001A A22E00        MOV     [002E],AL
...

But when you have more than a few data items this method is cumbersome, so the normal solution would be to perform the copy operation in a loop. When data is accessed using an address that is calculated at run time, the address must be stored in a register. For 32-bit code any of the general-purpose registers can be used, but for 16-bit code the register must be a base or index register. A copy operation involves both a source and destination address, so two registers are required. You could code the loop as shown above, or you could use a string move instruction:

    MOV DI,OFFSET COPY
    MOV SI,OFFSET DATA_IN
    MOV CX,06H
    CLD
    REP MOVSB

The CLD instruction ensures that the direction flag is cleared so SI and DI will be incremented as the move operation proceeds.

eschew obfuscation

UncannyDude

Yes, you can. Indeed, there are many ways to do it.

   MOV AX,@DATA
   MOV DS,AX
   MOV SI,OFFSET DATA_IN
   MOV CX,06H
   XOR  DX, DX
   
   
   AGAIN: MOV AL,[SI+DX]
          MOV [COPY+DX],AL
          INC DX
          LOOP AGAIN

MichaelW

Quote from: UncannyDude on February 03, 2005, 09:43:28 PM
Yes, you can. Indeed, there are many ways to do it.

   MOV AX,@DATA
   MOV DS,AX
   MOV SI,OFFSET DATA_IN
   MOV CX,06H
   XOR  DX, DX
   
   
   AGAIN: MOV AL,[SI+DX]
          MOV [COPY+DX],AL
          INC DX
          LOOP AGAIN


Yes, you can what? Your example will not assemble as 16-bit DOS code. For

MOV AL,[SI+DX]

MASM returns: error A2031: must be index or base register

And MASM assembles:

MOV [COPY+DX],AL

As:

0B43:000F 88B82C00      MOV     [BX+SI+002C],BH

But I do agree that there are many ways of doing it, and in cases where it would work the method you used in your example is good, efficient coding.



eschew obfuscation