AlmuHS AlmuHS - 1 month ago 16
Linux Question

How can i copy an array in nasm assembly?

I have to write a program which copy an array in other array, using x86 assembler

The original code is written in MSDOS' TASM for 8086 processor, but I want port this to Linux NASM using i386 processor

The code in TASM is this:

.MODEL SMALL

.DATA

TABLE_A DB 10, 5, 1
TABLE_B DB 0, 0, 0

.CODE

MOV AX, SEG TABLE_B
MOV DS, AX

MOV SI, 0

LOOP:
MOV AL, TABLE_A[SI]
MOV TABLE_B[SI], AL

INC SI
CMP SI, 2
JBE LOOP


MOV AH, 4Ch
INT 21h

END


I'm trying to rewrite this in nasm, but I don't get to sit in the correct array position, similar to TABLE_A[SI] instruction

How can I do it?

Answer

Use pointers (SI, DI) to the arrays and CX as counter :

MOV SI, Table_A     ;POINTER TO TABLE_A.
MOV DI, Table_B     ;POINTER TO TABLE_B.
MOV CX, 3           ;ARRAY LENGTH.
REPEAT:       
    MOV AL, [SI]
    MOV [DI], AL
    INC SI
    INC DI
    LOOP REPEAT     ;CX-1. IF CX>0 JUMP TO REPEAT.