Reverse Engineering for Beginners

(avery) #1

CHAPTER 18. ARRAYS CHAPTER 18. ARRAYS


; $a3=value
sll $v0, $a0, 5
; $v0 = $a0<<5 = x32
sll $a0, 3
; $a0 = $a0<<3 = x
8
addu $a0, $v0
; $a0 = $a0+$v0 = x8+x32 = x40
sll $v1, $a1, 5
; $v1 = $a1<<5 = y
32
sll $v0, $a0, 4
; $v0 = $a0<<4 = x4016 = x640
sll $a1, 1
; $a1 = $a1<<1 = y
2
subu $a1, $v1, $a1
; $a1 = $v1-$a1 = y32-y2 = y30
subu $a0, $v0, $a0
; $a0 = $v0-$a0 = x
640-x40 = x600
la $gp, __gnu_local_gp
addu $a0, $a1, $a0
; $a0 = $a1+$a0 = y30+x600
addu $a0, $a2
; $a0 = $a0+$a2 = y30+x600+z
; load address of table:
lw $v0, (a & 0xFFFF)($gp)
; multiply index by 4 to seek array element:
sll $a0, 2
; sum up multiplied index and table address:
addu $a0, $v0, $a0
; store value into table and return:
jr $ra
sw $a3, 0($a0)


.comm a:0x1770

18.6.4 More examples.


The computer screen is represented as a 2D array, but the video-buffer is a linear 1D array. We talk about it here:83.2 on
page 812.


18.7 Pack of strings as a two-dimensional array


Let’s revisit the function that returns the name of a month: listing.18.8. As you see, at least one memory load operation is
needed to prepare a pointer to the string that’s the month’s name. Is it possible to get rid of this memory load operation?
In fact yes, if you represent the list of strings as a two-dimensional array:


#include <stdio.h>
#include <assert.h>


const char month2[12][10]=
{
{ 'J','a','n','u','a','r','y', 0, 0, 0 },
{ 'F','e','b','r','u','a','r','y', 0, 0 },
{ 'M','a','r','c','h', 0, 0, 0, 0, 0 },
{ 'A','p','r','i','l', 0, 0, 0, 0, 0 },
{ 'M','a','y', 0, 0, 0, 0, 0, 0, 0 },
{ 'J','u','n','e', 0, 0, 0, 0, 0, 0 },
{ 'J','u','l','y', 0, 0, 0, 0, 0, 0 },
{ 'A','u','g','u','s','t', 0, 0, 0, 0 },
{ 'S','e','p','t','e','m','b','e','r', 0 },
{ 'O','c','t','o','b','e','r', 0, 0, 0 },
{ 'N','o','v','e','m','b','e','r', 0, 0 },
{ 'D','e','c','e','m','b','e','r', 0, 0 }
};


// in 0..11 range
const char* get_month2 (int month)

Free download pdf