forked from cirosantilli/x86-assembly-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddressing.S
122 lines (96 loc) · 2.6 KB
/
addressing.S
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
# Addressing
Documented at:
https://sourceware.org/binutils/docs-2.18/as/i386_002dMemory.html
Of form:
s:a(b, c, d)
is the same as Intel notation:
s:[b + c*d + a]
Then GAS has various syntaxes for omitting parts of the address.
It's a bit harder to remember than AT&T.
*/
#include "lib/asm_io_s.h"
.data
i:
.long 0
ENTRY
/*
It is easier to use lea to do the unit tests.
Wherever a number is used, the label `i` could be used as well:
remember that labels are just translated to addresses.
*/
/*
Full form with immediates:
ebx + ecx * 2 + 4 =
3 + 4 * 2 + 4 =
3 + 8 + 4 =
3 + 8 + 4 =
15
*/
mov $0, %eax
mov $3, %ebx
mov $4, %ecx
/* GAS 2.24 Warning: segment override on `lea' is ineffectual. */
/*lea %ds:4(%ebx, %ecx, 2), %eax*/
lea 4(%ebx, %ecx, 2), %eax
ASSERT_EQ($15)
/*
Omit the mulitplicator d.
a(b,c) == a(b,c,1)
*/
mov $0, %eax
mov $3, %ebx
mov $4, %ecx
lea 2(%ebx, %ecx), %eax
ASSERT_EQ($9)
/* Omit c and d. */
mov $0, %eax
mov $1, %ebx
lea 2(%ebx), %eax
ASSERT_EQ($3)
/* Register only address. We can omit commas. */
mov $0, %eax
mov $1, %ebx
lea (%ebx), %eax
ASSERT_EQ($1)
/* TODO What is this syntax for? Compare to the next example. */
mov $0, %eax
lea 2(,1), %eax
ASSERT_EQ($2)
mov $0, %eax
lea 2, %eax
ASSERT_EQ($2)
/* TODO What is this syntax for? Compare to the previous example. */
mov $0, %eax
lea (2), %eax
ASSERT_EQ($2)
mov $0, %eax
mov $3, %ebx
lea 2(,%ebx,4), %eax
ASSERT_EQ($14)
/*
Expressions like (1 + 1) or more commonly (label + 1)
can be used like anywhere else.
*/
mov $1, %eax
lea (1 + 1)(%eax), %eax
ASSERT_EQ($3)
/*
Impossible addressings.
AT&T syntax requires you to thing more about can or cannot be done.
*/
/*lea (%bl), %eax*/
/*
Now some examples with the label and actual memory movs just for concreteness.
*/
/* Simple memory IO. */
mov $0, %eax
/* `l` suffix needed here because there is no register to tell the size. */
movl $1, i
mov i, %eax
ASSERT_EQ($1)
/* To get the actual address instead of the data, use `$`: */
mov $i, %ebx
mov (%ebx), %eax
ASSERT_EQ(i)
EXIT