Reputation: 59
I need to declare two global dword pointers that point to two arrays. To my knowledge, global means declare in .data. Is that correct? What is the code to declare these dword pointers so that the are initialized to 0 in x86 assembly with NASM and Intel syntax?
Upvotes: 3
Views: 1295
Reputation: 1019
I need to declare two global dword pointers that point to two arrays.
That's simple if i understand correctly, assuming they mean global as in to all files, make a file called pointers.asm
or something and type:
[section] .bss
global _pointer1, _pointer2
_pointer1 resd 4 ; reserve 4 dwords
_pointer2 resd 4
We use a .bss
section because that memory is set to zero so when you use it your variables are 0 initialized
Or you could just use a .data
section if you want and initialize each element to zero yourself:
[section] .data
global _pointer1, _pointer2
_pointer1 dd 0,0,0,0
_pointer2 dd 0,0,0,0
Also still using a .data
section, it could be done like this allowing you to specify the size of a buffer like with the .bss
section:
[section] .data
global _pointer1, _pointer2
_pointer1 times 4 dd 0
_pointer2 times 4 dd 0
Regardless of the way you decide to do it, to use a pointer to an array declared globally in a separate file:
[section] .text
global _main
extern _pointer1
bytesize equ 4 ; dword = 4 bytes
_main:
; write each element of the array
mov dword [_pointer1 + 0 * bytesize], 0xa
mov dword [_pointer1 + 1 * bytesize], 0xb
mov dword [_pointer1 + 2 * bytesize], 0xc
mov dword [_pointer1 + 3 * bytesize], 0xd
; read each element of the array
mov eax, [_pointer1 + 0 * bytesize]
mov eax, [_pointer1 + 1 * bytesize]
mov eax, [_pointer1 + 2 * bytesize]
mov eax, [_pointer1 + 3 * bytesize]
ret
This main program returns with 0xd
or 13
stored in eax
, hopefully by looking at this you can get a grasp what is going on.
Upvotes: 3