Assembly Language for Beginners

(Jeff_L) #1

1.5 Hello, world!.


There are just two instructions: the first places the value 123 into theEAXregister, which is used by
convention for storing the return value, and the second one isRET, which returns execution to thecaller.


The caller will take the result from theEAXregister.


1.4.2 ARM.


There are a few differences on the ARM platform:


Listing 1.10: Optimizing Keil 6/2013 (ARM mode) ASM Output

f PROC
MOV r0,#0x7b ; 123
BX lr
ENDP


ARM uses the registerR0for returning the results of functions, so 123 is copied intoR0.


It is worth noting thatMOVis a misleading name for the instruction in both the x86 and ARMISAs.


The data is not in factmoved, butcopied.


1.4.3 MIPS.


The GCC assembly output below lists registers by number:


Listing 1.11: Optimizing GCC 4.4.5 (assembly output)
j $31
li $2,123 # 0x7b

...whileIDAdoes it by their pseudo names:


Listing 1.12: Optimizing GCC 4.4.5 (IDA)
jr $ra
li $v0, 0x7B

The $2 (or $V0) register is used to store the function’s return value.LIstands for “Load Immediate” and
is the MIPS equivalent toMOV.


The other instruction is the jump instruction (J or JR) which returns the execution flow to thecaller.


You might be wondering why the positions of the load instruction (LI) and the jump instruction (J or JR) are
swapped. This is due to aRISCfeature called “branch delay slot”.


The reason this happens is a quirk in the architecture of some RISCISAs and isn’t important for our
purposes—wemustsimplykeepinmindthatinMIPS,theinstructionfollowingajumporbranchinstruction
is executedbeforethe jump/branch instruction itself.


As a consequence, branch instructions always swap places with the instruction executed immediately
beforehand.


In practice, functions which merely return 1 (true) or 0 (false) are very frequent.


The smallest ever of the standard UNIX utilities,/bin/trueand/bin/falsereturn 0 and 1 respectively, as an
exit code. (Zero as an exit code usually means success, non-zero means error.)


1.5 Hello, world!


Let’s use the famous example from the book [Brian W. Kernighan, Dennis M. Ritchie,The C Programming
Language, 2ed, (1988)]:

Free download pdf