1. 背景
很多应用程序中都会动态申请内存用于存储中间数据、文件数据等。在元神操作系统中可以通过API调用来申请动态内存,获取可用的内存起始地址和内存大小。
2. 方法
(1)编写程序
本例通过调用API_ALLOC_MEMORY这个API来获取可用内存的起始地址和内存大小,代码如下所示:
use32
START:
pusha
call demo_mem_alloc
popa
iret
include 'api_def.inc'
OS_API equ 0x00030C16
API_PARAM equ 0x03000000
cursor_x equ 0x02004B10
cursor_y equ 0x02004B12
demo_mem_alloc:
pusha
mov edi, API_PARAM
mov dword [fs:edi], API_ALLOC_MEMORY
mov dword [fs:edi+4], 0
call pword [fs:OS_API]
mov eax, [fs:edi+8]
movzx ebx, word [fs:cursor_y]
movzx ecx, word [fs:cursor_x]
call print_dword ;print start address of memory for allocation
mov eax, [fs:edi+12]
add ecx, 10
call print_dword ;print memory size for allocation
add word [fs:cursor_y], 1
popa
ret
;print dword value
;input:
; eax: value to print
; ebx: y position to print
; ecx: x position to print
print_dword:
pusha
imul edi, ebx, 80*2
add edi, ecx
add edi, ecx
add edi, 14
mov ecx,