Thursday, March 11, 2021

Using Scale Factors

To extract an array element, we normally compute the number of bytes from beginning of data segment.


.data
myArray BYTE 10h,20h,30h,40h

.code
mov al,myArray     ; gets 10h
mov al,myArray+2   ; gets 30h


Extracting element from a DWORD array might be tough.


.data
myArray DWORD 10h,20h,30h,40h,50h,60h

.code
mov eax,myArray     ; gets 10h
mov eax,myArray+4   ; gets 20h
mov eax,myArray+12  ; gets 40h


With scale factors, this job will be easy. This code will work in any array sizes and you just need to specify the subscript (element you want to extract) via ESI.


.data
myArray DWORD 10h,20h,30h,40h,50h,60h

.code
mov esi,3                          ; points to element 3
mov eax,myArray[esi*type myArray]  ; gets 40h

No comments:

Post a Comment