-
Notifications
You must be signed in to change notification settings - Fork 0
/
cApp_29.asm
154 lines (112 loc) · 4.13 KB
/
cApp_29.asm
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
comment @ «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
The standard Intel notation for addressing memory can look very daunting
to a beginner but it is in fact very compact and simple enough to use
once you know how it works. It is usually referred to as the "complex"
addressing modes. If you understand it properly you can write very compact
and fast code using the technique.
When you have code like,
mov eax, [ebx+ecx*4+32]
The section enclosed in square brackets is broken up in the following
manner.
[Base Address + Index * Scale + Displacement]
Base address
The starting address in memory
Index
A 32 bit register which is the variable for changing the address
Scale
The data size being worked on, it can be 1, 2, 4 or 8
Displacement
An optional additional offset to change the address by.
The example below will show how it works.
««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««« @
.486 ; create 32 bit code
.model flat, stdcall ; 32 bit memory model
option casemap :none ; case sensitive
include \masm32\include\windows.inc ; always first
include \masm32\macros\macros.asm ; MASM support macros
; -----------------------------------------------------------------
; include files that have MASM format prototypes for function calls
; -----------------------------------------------------------------
include \masm32\include\masm32.inc
include \masm32\include\gdi32.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
; ------------------------------------------------
; Library files that have definitions for function
; exports and tested reliable prebuilt code.
; ------------------------------------------------
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\gdi32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
.data
; --------------------------
; initialise 10 DWORD values
; --------------------------
itm0 dd 0
itm1 dd 1
itm2 dd 2
itm3 dd 3
itm4 dd 4
itm5 dd 5
itm6 dd 6
itm7 dd 7
itm8 dd 8
itm9 dd 9
; ---------------------------------
; put their addresses into an array
; ---------------------------------
array dd itm0,itm1,itm2,itm3,itm4
dd itm5,itm6,itm7,itm8,itm9
.code ; Tell MASM where the code starts
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start: ; The CODE entry point to the program
call main ; branch to the "main" procedure
mainLoop:
push 1Bh
call GetAsyncKeyState
cmp eax, 0h
jne exitMainLoop
push 064h
call Sleep
jmp mainLoop
exitMainLoop:
ret
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
main proc
LOCAL cnt :DWORD ; allocate a loop counter
push ebx
push esi
push edi
mov cnt, 10 ; set the number of loop iterations
mov ebx, array ; put BASE ADDRESS of array in EBX
xor esi, esi ; Use ESI as INDEX and set to zero
print chr$("Index being changed",13,10)
label2:
mov edi, [ebx+esi*4]
print str$(edi)
print chr$(13,10)
add esi, 1 ; each array member is accessed by changing the INDEX
sub cnt, 1
jnz label2
print chr$("Displacement being changed",13,10)
xor esi, esi
mov edi, [ebx+esi*4] ; no displacement
print str$(edi)
print chr$(13,10)
mov edi, [ebx+esi*4+4] ; added displacement of 4 bytes
print str$(edi)
print chr$(13,10)
mov edi, [ebx+esi*4+8] ; added displacement of 8 bytes
print str$(edi)
print chr$(13,10)
mov edi, [ebx+esi*4+12] ; added displacement of 12 bytes
print str$(edi)
print chr$(13,10)
pop edi
pop esi
pop ebx
ret
main endp
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start ; Tell MASM where the program ends