当前位置: 首页 > 工具软件 > FASM > 使用案例 >

Fasm之堆和栈操作

卢朝
2023-12-01

用过fasm之后感觉对代码在CPU上运行原理更理解了。

data_source_init:
        push    ebp
         mov    ebp, esp              ; 保存函数入口栈指针,也可以用enter指令。

      invoke    GetProcessHeap
      invoke    HeapAlloc, eax, HEAP_ZERO_MEMORY, 64
          or    eax, eax
          jz    @F

         mov    ebx, [ebp + 12]       ; 对象指针变量指针。第三个参数放到ebx。
         mov    [ebx], eax            ; 保存对象指针。

         mov    ebx, [ebp + 8]        ; 对象指针。2nd参数
         mov    [eax], ebx            ; 输入文件句柄。
         mov    dword [eax + 4], 12   ; 输入的数据最小长度。1st参数

        push    eax
      invoke    GetProcessHeap
      invoke    HeapAlloc, eax, HEAP_ZERO_MEMORY, 512
         pop    ebx
         mov    [ebx + 12], eax
          or    eax, eax
          jz    .data_source_init_failed

         mov    eax, ebx
         jmp    @F

  .data_source_init_failed:
        push    dword [ebp + 8]     ; 参数入栈。
        call    sqlite3_adapter_destroy
         xor    eax, eax
         @@:
       leave                        ; 专用指令,恢复栈平衡。C语言中的stdcall方式。也可以用mov esp, ebp; pop ebp
        retn    8

data_source_destroy:
        push    ebp
         mov    ebp, esp
         mov    eax, [ebp + 8]
          or    eax, eax
          jz    .data_source_destroy_finish
         mov    ebx, [eax]
          or    ebx, ebx
          jz    .data_source_destroy_finish

         mov    eax, [ebx + 12]
          or    eax, eax
          jz    @F
        push    ebx
        push    eax
        push    0
        call    [GetProcessHeap]
        push    eax
        call    [HeapFree]
         pop    ebx
         @@:

        push    ebx
        push    0
        call    [GetProcessHeap]
        push    eax
        call    [HeapFree]

         xor    ebx, ebx
         mov    eax, [ebp + 8]
         mov    [eax], ebx

  .data_source_destroy_finish:
       leave
        retn    4

data_source_wait_input:
        push    ebp
         mov    ebp, esp
         sub    esp, 4               ; 声音一个DWORD变量。
        push    0
        lea     eax, [ebp - 4]
        push    eax
        push    512
        mov     ebx, [ebp + 8]
        push    dword [ebx + 12]
        push    dword [ebx]
        call    [ReadConsole]
        test    eax, eax
          jz    @F
        mov     eax, [ebp + 8]
        mov     ebx, [eax + 12]
         mov    eax, [ebp - 4]
         mov    byte [ebx + eax], 0
         @@:
       leave
         ret    4

data_source_parse:
        push    ebp
         mov    ebp, esp
        mov     ebx, [ebp + 8]
         invoke MessageBox,0,0,dword [ebx + 12],0
       leave
        retn    4

sqlite3_adapter_init:
        push    ebp
         mov    ebp, esp

      invoke    GetProcessHeap
      invoke    HeapAlloc, eax, HEAP_ZERO_MEMORY, 64
          or    eax, eax
          jz    @F

         mov    ebx, [ebp + 12]
         mov    [ebx], eax

        push    eax
      invoke    GetProcessHeap
      invoke    HeapAlloc, eax, HEAP_ZERO_MEMORY, 256
         pop    ebx
         mov    [ebx + 4], eax
          or    eax, eax
          jz    .sqlite3_adapter_init_failed

        push    dword [ebp + 8]
        push    eax
        call    [lstrcpy]
  
        push    dword [ebp + 12]
        push    dword [ebp + 8]
        call    [sqlite3_open]
          or    eax, eax
         jnz    .sqlite3_adapter_init_failed

         mov    eax, ebx
         jmp    @F

  .sqlite3_adapter_init_failed:
        push    dword [ebp + 8]
        call    sqlite3_adapter_destroy
         xor    eax, eax
         @@:
       leave
        retn    8

sqlite3_adapter_destroy:
        push    ebp
         mov    ebp, esp

         mov    eax, [ebp + 8]
          or    eax, eax
          jz    .sqlite3_adapter_destroy_finish
         mov    ebx, [eax]
          or    ebx, ebx
          jz    .sqlite3_adapter_destroy_finish

         mov    eax, [ebx + 4]
          or    eax, eax
          jz    @F
        push    ebx
        push    eax
        push    0
        call    [GetProcessHeap]
        push    eax
        call    [HeapFree]
         pop    ebx
         @@:

         mov    eax, [ebx]
          or    eax, eax
          jz    @F
        push    ebx
        push    eax
        call    [sqlite3_close]
         pop    ebx
         xor    eax, eax
         mov    [ebx], eax
         @@:

  .sqlite3_adapter_destroy_finish:
       leave
        retn    4


 类似资料: