Direct addressing

Just as in the case of code addressing, this mode implies that the address of either the source or destination operand (depending on the instruction and intention) is explicitly specified. However, unlike code addressing, we are able to specify the address itself, except when loading it into a register first. Consider the example of loading the value of a variable into a register or storing it from register to memory:

 mov al, [name_of_variable]
; or
mov [name_of_another_variable], eax

In both cases, name_of_variable and name_of_another_variable are translated by the assembler into the addresses of those variables. Of course, we may also use registers for this purpose. The following example illustrates an if…else clause:

; This goes into code section.
xor rax, rax
; inc rax ; Increment RAX in order to call the second procedure
lea rbx, [indices]
add rax, rbx
lea rbx, [my_proc_address]
add bl, [rax]
mov rbx, [rbx]
call qword rbx
; The rest of the code

align 8
my_proc0:
push rbp
mov rbp, rsp
xor eax, eax
mov rsp, rbp
pop rbp
ret

align 8
my_proc1:
push rbp
mov rbp, rsp
xor eax, eax
inc eax
mov rsp, rbp
pop rbp
ret

; And the following goes into data section
indices db 0, 8
align 8
my_proc_address dq my_proc0, my_proc1

The first line of the code sets the rax register to zero, which, when the second line is commented out, causes the code to call my_proc0. On the other hand, if we uncomment the inc rax instruction, then my_proc1 would be called instead.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset