如何在nasm-syntax asm代码中定义和使用“数组结构”?

时间:2014-02-19 16:37:28

标签: winapi assembly nasm masm seh

所以基本上我在Win32上处理伪asm代码,我试图让它重新组装。

在代码中,我看到如下数据结构:

errtable    errentry <1, 16h>
            errentry <2, 2>
            errentry <3, 2>
            errentry <4, 18h>
            errentry <5, 0Dh>
            .....

在.text部分,我看到这样的代码:

cmp     eax, dword ptr errtable.oscode[ecx*8]
mov     eax, errtable.errnocode[ecx*8]

基本上asm代码/数据如上所述不能由NASM / MASM在Windows 32位上直接重新组装。所以我必须调整这段代码/数据......

我在线搜索并在C中找到数据结构的定义:

struct errentry {
    unsigned long oscode;           /* OS return value */
    int errnocode;  /* System V error code */
};

static struct errentry errtable[] = {
    {  ERROR_INVALID_FUNCTION,       EINVAL    },  /* 1 */
    {  ERROR_FILE_NOT_FOUND,         ENOENT    },  /* 2 */
    {  ERROR_PATH_NOT_FOUND,         ENOENT    },  /* 3 */
    {  ERROR_TOO_MANY_OPEN_FILES,    EMFILE    },  /* 4 */
    {  ERROR_ACCESS_DENIED,          EACCES    },  /* 5 */
    {  ERROR_INVALID_HANDLE,         EBADF     },  /* 6 */
    {  ERROR_ARENA_TRASHED,          ENOMEM    },  /* 7 */
    {  ERROR_NOT_ENOUGH_MEMORY,      ENOMEM    },  /* 8 */
    .........

所以我的问题是:如何修改asm代码/数据以使其重新组合?

谢谢!

1 个答案:

答案 0 :(得分:1)

您标记了,您需要哪些?我确信这可以通过某种宏来在NASM中完成,但在MASM中它是轻而易举的。

ENOENT      equ 2
EBADF       equ 9
ENOMEM      equ 12
EACCES      equ 13
EINVAL      equ 22
EMFILE      equ 24

errentry struc
    oscode      dd  ?
    ernocode    dd  ?
errentry ends

.data
errtable    errentry    <ERROR_INVALID_FUNCTION, EINVAL>, \
                        <ERROR_FILE_NOT_FOUND, ENOENT>, \
                        <ERROR_PATH_NOT_FOUND, ENOENT>, \
                        <ERROR_TOO_MANY_OPEN_FILES, EMFILE>, \
                        <ERROR_ACCESS_DENIED, EACCES>, \
                        <ERROR_INVALID_HANDLE, EBADF>, \
                        <ERROR_ARENA_TRASHED, ENOMEM>, \
                        <ERROR_NOT_ENOUGH_MEMORY, ENOMEM>
                        ; ...
.code
start:

NextCode:
    cmp     eax, errtable.oscode[ecx * 8]
    jne     NotFound

    mov     eax, errtable.ernocode[ecx * 8]
    jmp     Next

NotFound:
    ; Err number not found, return something

Next:

和一些测试代码:
我们将更改寄存器,因此我们不需要将它们保存在printf

附近
include masm32rt.inc

ENOENT      equ 2
EBADF       equ 9
ENOMEM      equ 12
EACCES      equ 13
EINVAL      equ 22
EMFILE      equ 24

errentry struc
    oscode      dd  ?
    ernocode    dd  ?
errentry ends

.data
fmtint      db  "OS Code = %d, Err Code = %d", 13, 10, 0
fmtstr      db  "OS Code %d NOT FOUND!!!", 13, 10, 0

errtable    errentry    <ERROR_INVALID_FUNCTION, EINVAL>, \
                        <ERROR_FILE_NOT_FOUND, ENOENT>, \
                        <ERROR_PATH_NOT_FOUND, ENOENT>, \
                        <ERROR_TOO_MANY_OPEN_FILES, EMFILE>, \
                        <ERROR_ACCESS_DENIED, EACCES>, \
                        <ERROR_INVALID_HANDLE, EBADF>, \
                        <ERROR_ARENA_TRASHED, ENOMEM>, \
                        <ERROR_NOT_ENOUGH_MEMORY, ENOMEM>
                        ; ...
errtable_len    equ ($ - errtable) / 8

.code
start:

    mov     edi, 1
    xor     ebx, ebx
NextCode:
    cmp     edi, errtable.oscode[ebx * 8]
    jne     NotFound

    invoke  crt_printf, offset fmtint, errtable.oscode[ebx * 8], errtable.ernocode[ebx * 8]
    jmp     Next

NotFound:
    ; Err number not found, return something 
    invoke  crt_printf, offset fmtstr, edi 


Next:
    inc     ebx
    inc     edi
    cmp     edi, 12 ; loop 3 more than our test array
    jne     NextCode

    inkey
    invoke  ExitProcess, 0
end start

enter image description here