Assembly Language for Beginners

(Jeff_L) #1

1.20. ARRAYS


That’s fine for commutative operations like addition or multiplication (operands may be swapped there
without changing the result).


But subtraction is a non-commutative operation, soRSBexist for these cases.


MIPS


My example is tiny, so the GCC compiler decided to put theaarray into the 64KiB area addressable by
the Global Pointer.


Listing 1.253: Optimizing GCC 4.4.5 (IDA)

insert:
; $a0=x
; $a1=y
; $a2=z
; $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

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:8.13.2 on page 916.


Another example in this book is Minesweeper game: it’s field is also two-dimensional array:8.3.


1.20.7 Pack of strings as a two-dimensional array


Let’s revisit the function that returns the name of a month: listing.1.232.


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?

Free download pdf