Assembly Language for Beginners

(Jeff_L) #1
1.9. SCANF()
So, thexvariable is now global and for this reason located in another segment, namely the data segment
(.data). One could ask, why are the text strings located in the code segment (.text) andxis located right
here? Because it is a variable and by definition its value could change. Moreover it could possibly change
often. While text strings has constant type, they will not be changed, so they are located in the.text
segment.

ThecodesegmentmightsometimesbelocatedinaROM^74 chip(keepinmind,wenowdealwithembedded
microelectronics, and memory scarcity is common here), and changeable variables —inRAM.

It is not very economical to store constant variables in RAM when you have ROM.

Furthermore,constantvariablesinRAMmustbeinitialized,becauseafterpoweringon,theRAM,obviously,
contains random information.

Moving forward, we see a pointer to thex(off_2C) variable in the code segment, and that all operations
with the variable occur via this pointer.

That is because thexvariable could be located somewhere far from this particular code fragment, so its
address must be saved somewhere in close proximity to the code.

TheLDRinstruction in Thumb mode can only address variables in a range of 1020 bytes from its location,

and in ARM-mode —variables in range of± 4095 bytes.

And so the address of thexvariable must be located somewhere in close proximity, because there is no
guarantee that the linker would be able to accommodate the variable somewhere nearby the code, it may
well be even in an external memory chip!

Onemorething: ifavariableisdeclaredasconst, theKeilcompilerallocatesitinthe.constdatasegment.

Perhaps thereafter, the linker could place this segment in ROM too, along with the code segment.

ARM64

Listing 1.76: Non-optimizing GCC 4.9.1 ARM64
1 .comm x,4,4
2 .LC0:
3 .string "Enter X:"
4 .LC1:
5 .string "%d"
6 .LC2:
7 .string "You entered %d...\n"
8 f5:
9 ; save FP and LR in stack frame:
10 stp x29, x30, [sp, -16]!
11 ; set stack frame (FP=SP)
12 add x29, sp, 0
13 ; load pointer to the "Enter X:" string:
14 adrp x0, .LC0
15 add x0, x0, :lo12:.LC0
16 bl puts
17 ; load pointer to the "%d" string:
18 adrp x0, .LC1
19 add x0, x0, :lo12:.LC1
20 ; form address of x global variable:
21 adrp x1, x
22 add x1, x1, :lo12:x
23 bl __isoc99_scanf
24 ; form address of x global variable again:
25 adrp x0, x
26 add x0, x0, :lo12:x
27 ; load value from memory at this address:
28 ldr w1, [x0]
29 ; load pointer to the "You entered %d...\n" string:
30 adrp x0, .LC2
31 add x0, x0, :lo12:.LC2
32 bl printf
33 ; return 0
34 mov w0, 0


(^74) Read-Only Memory

Free download pdf