Using 16-bit addresses like foo[si] with Irvine32 (in 32-bit mode)?

Viewed 62

For some reason, while I was debugging it stopped at mov al,data1[si] and gave me an exception

Exception thrown at 0x00D53668 in Week 5.exe: 0xC0000005: Access violation reading location 0x01AA6000.

May I know what was the reason?

INCLUDE Irvine32.inc

.data

data1 byte "MILK",0
data2 byte 4 dup('*'),0


.code
main proc

mov CX,4
mov si,0


l1: 
mov al,data1[si]  <<EXCEPTION THROWN AT THIS LINE>>

mov data2[si],al
    
inc si  ; before go in the loop l1, cx will auto deduct by 1, then cx==0?
    
loop l1
    
mov EDX, offset data2
call WriteString
    
exit
main endp
end main
1 Answers

You have tagged your question and which indicates that you are using 32-bit instructions, but are trying to use a 16-bit index register(=SI) in

mov al,data1[si]

This is (almost) guaranteed to fail. Use (and setup before) a 32-bit register like ESI instead:

mov al, byte ptr data1[esi]

If you're writing 32-bit code, you should generally use the 32-bit registers for your normal program flow. And the 16-bit ones only on rare, well-thought occasions.

Related