0

此代码是反汇编成汇编的 C 程序(冒泡排序)。如果我将以下代码放在 .asm 文件中并使用 nasm 进行汇编,如何使以下代码运行?如果您知道需要更改的内容,请说出要更改的内容。例如,我知道 nasm 不会接受DWORD PTR,但我还没有找到该怎么做。谢谢

.file   "sort.c" .intel_syntax noprefix .text .globl 
sort .type   sort, @function
sort: .LFB0:
    .cfi_startproc
    push    rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    mov rbp, rsp
    .cfi_def_cfa_register 6
    mov QWORD PTR [rbp-24], rdi
    mov DWORD PTR [rbp-28], esi
    mov DWORD PTR [rbp-12], 0
    jmp .L2
.L6:
    mov DWORD PTR [rbp-8], 0
    jmp .L3
.L5:
    mov eax, DWORD PTR [rbp-8]
    cdqe
    sal rax, 2
    add rax, QWORD PTR [rbp-24]
    mov edx, DWORD PTR [rax]
    mov eax, DWORD PTR [rbp-8]
    cdqe
    add rax, 1
    sal rax, 2
    add rax, QWORD PTR [rbp-24]
    mov eax, DWORD PTR [rax]
    cmp edx, eax
    jle .L4
    mov eax, DWORD PTR [rbp-8]
    cdqe
    sal rax, 2
    add rax, QWORD PTR [rbp-24]
    mov eax, DWORD PTR [rax]
    mov DWORD PTR [rbp-4], eax
    mov eax, DWORD PTR [rbp-8]
    cdqe
    sal rax, 2
    add rax, QWORD PTR [rbp-24]
    mov edx, DWORD PTR [rbp-8]
    movsx   rdx, edx
    add rdx, 1
    sal rdx, 2
    add rdx, QWORD PTR [rbp-24]
    mov edx, DWORD PTR [rdx]
    mov DWORD PTR [rax], edx
    mov eax, DWORD PTR [rbp-8]
    cdqe
    add rax, 1
    sal rax, 2
    add rax, QWORD PTR [rbp-24]
    mov edx, DWORD PTR [rbp-4]
    mov DWORD PTR [rax], edx
.L4:
    add DWORD PTR [rbp-8], 1
.L3:
    mov eax, DWORD PTR [rbp-28]
    sub eax, 1
    sub eax, DWORD PTR [rbp-12]
    cmp eax, DWORD PTR [rbp-8]
    jg  .L5
    add DWORD PTR [rbp-12], 1
.L2:
    mov eax, DWORD PTR [rbp-28]
    sub eax, 1
    cmp eax, DWORD PTR [rbp-12]
    jg  .L6
    pop rbp
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE0:
    .size   sort, .-sort
    .ident  "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
    .section    .note.GNU-stack,"",@progbits
4

1 回答 1

0

只需删除PTR所有无意义.somethings的 .

这组装得很好:

; file: gas-nasm-sort.asm
bits 64

sort:
push rbp
mov rbp, rsp
mov QWORD [rbp-24], rdi
mov DWORD [rbp-28], esi
mov DWORD [rbp-12], 0
jmp .L2

.L6:
mov DWORD [rbp-8], 0
jmp .L3

.L5:
mov eax, DWORD [rbp-8]
cdqe
sal rax, 2
add rax, QWORD [rbp-24]
mov edx, DWORD [rax]
mov eax, DWORD [rbp-8]
cdqe
add rax, 1
sal rax, 2
add rax, QWORD [rbp-24]
mov eax, DWORD [rax]
cmp edx, eax
jle .L4
mov eax, DWORD [rbp-8]
cdqe
sal rax, 2
add rax, QWORD [rbp-24]
mov eax, DWORD [rax]
mov DWORD [rbp-4], eax
mov eax, DWORD [rbp-8]
cdqe
sal rax, 2
add rax, QWORD [rbp-24]
mov edx, DWORD [rbp-8]
movsx rdx, edx
add rdx, 1
sal rdx, 2
add rdx, QWORD [rbp-24]
mov edx, DWORD [rdx]
mov DWORD [rax], edx
mov eax, DWORD [rbp-8]
cdqe
add rax, 1
sal rax, 2
add rax, QWORD [rbp-24]
mov edx, DWORD [rbp-4]
mov DWORD [rax], edx

.L4:
add DWORD [rbp-8], 1

.L3:
mov eax, DWORD [rbp-28]
sub eax, 1
sub eax, DWORD [rbp-12]
cmp eax, DWORD [rbp-8]
jg .L5
add DWORD [rbp-12], 1

.L2:
mov eax, DWORD [rbp-28]
sub eax, 1
cmp eax, DWORD [rbp-12]
jg .L6
pop rbp
ret

命令:

nasm gas-nasm-sort.asm -f bin -o gas-nasm-sort.bin

但同样,有 NASM 文档。阅读。特别是这些部分:

  • 2.2.2 NASM 需要方括号用于内存引用

  • 2.2.3 NASM 不存储变量类型

于 2012-07-18T04:07:26.483 回答