Reverse Engineering for Beginners

(avery) #1

CHAPTER 15. SIMPLE C-STRINGS PROCESSING CHAPTER 15. SIMPLE C-STRINGS PROCESSING


add sp, sp, 32
ret

It’s more verbose. The variables are often tossed here to and from memory (local stack). The same mistake here: the
decrement operation happens on a 32-bit register part.


15.1.3 MIPS.


Listing 15.5: Optimizing GCC 4.4.5 (IDA)

my_strlen:
; "eos" variable will always reside in $v1:
move $v1, $a0


loc_4:
; load byte at address in "eos" into $a1:
lb $a1, 0($v1)
or $at, $zero ; load delay slot, NOP
; if loaded byte is not zero, jump to loc_4:
bnez $a1, loc_4
; increment "eos" anyway:
addiu $v1, 1 ; branch delay slot
; loop finished. invert "str" variable:
nor $v0, $zero, $a0
; $v0=-str-1
jr $ra
; return value = $v1 + $v0 = eos + ( -str-1 ) = eos - str - 1
addu $v0, $v1, $v0 ; branch delay slot


MIPS lacks aNOTinstruction, but hasNORwhich isOR + NOToperation. This operation is widely used in digital electronics^5 ,
but isn’t very popular in computer programming. So, the NOT operation is implemented here asNOR DST, $ZERO, SRC.


From fundamentals30 on page 431we know that bitwise inverting a signed number is the same as changing its sign and
subtracting 1 from the result. So whatNOTdoes here is to take the value ofstrand transform it into−str− 1. The addition
operation that follows prepares result.


(^5) NOR is called “universal gate”. For example, the Apollo Guidance Computer used in the Apollo program, was built by only using 5600 NOR gates: [Eic11].

Free download pdf