consciousOutsider
consciousOutsider

Reputation: 25

Assembly x86 infinite loop issue MOV CX, 12 after Procedure RET

NUM EQU 3
.MODEL small
.STACK
.DATA
    tempi DW 8,27,17,12,21,34,9,41,7,18,15,5
    risultato DW NUM DUP (?)
.CODE
.STARTUP
    PUSH OFFSET tempi
    PUSH OFFSET risultato
    CALL copiaVettore
    ADD SP,4
    MOV CX, 0
    MOV CX,12
    LEA BX, risultato
salta:
    MOV DX, [BX]
    ADD DX, 30h
    ADD BX, 2 
    MOV AH, 2h
    INT 21h
LOOP salta
.EXIT
 copiaVettore PROC
    PUSH BP 
    MOV BP, SP
    PUSH DI
    PUSH SI

    MOV DI, [BP+4] ;risultato
    MOV SI, [BP+6] ;sorgente    
    MOV CX, 12
    ciclo1: 
        MOV AX, [SI]
        MOV [DI], AX 
        ADD DI, 2
        ADD SI, 2
    LOOP ciclo1

    POP SI
    POP DI
    POP BP
RET
copiaVettore ENDP 
END

When the procedure RETurns, the debugger stops on MOV CX, 12 with

unknown opcode skipped: 00 not 8086 instruction - not supported yet.

but the MOV CX, 12 inside the procedure works well.

Upvotes: 2

Views: 770

Answers (1)

Tommylee2k
Tommylee2k

Reputation: 2731

nice example of clobbering. Most people clobber registers, you "clobber"ed your code instead :)

NUM EQU 3          ; <-- here's the problem
.MODEL small
.STACK
.DATA
    tempi DW 8,27,17,12,21,34,9,41,7,18,15,5  ;  12 words source
    risultato DW NUM DUP (?)                  ;  but only 3 words data

since you have only 3 words room for the copy, the 9 following words are written over the code, destroying the code the RET returns to

.CODE
.STARTUP
    PUSH OFFSET tempi       ; 3 bytes 12,0,21
    PUSH OFFSET risultato   ; 3 bytes 0,34,0
    CALL copiaVettore       ; 3 bytes 9,0,14
    ADD SP,4                ; <-- this is where the RET will return, 
    MOV CX, 0               ;     but starting from here the code 
    MOV CX,12               ;     is overwritten with 0,7,0,18 ...
    LEA BX, risultato

so the code you expect isn't there anymore.

Upvotes: 7

Related Questions