简体   繁体   English

NASM mov从寄存器到内存

[英]NASM mov from register to memory

I know there are lots of references out there talking about NASM and mov but either I'm missing something fundamental or people need to write better help guides! 我知道有很多关于NASM和mov的参考资料,但是我缺少基本知识,或者人们需要编写更好的帮助指南!

SECTION .data
    fmtStart:       db "Enter two numbers in format '# #'", 10, 0
    fmtTest:        db "sum: %d", 10, 0
    input:          db "%d %d", 0
SECTION .bss                ; BSS, uninitialized variables
    int1:       resd 1
    int2:       resd 1
    sum:        resd 1
SECTION .text               ; Code section.
    global main             ; the standard gcc entry point
    main:                   ; the program label for the entry point
    push ebp            ; set up stack frame
    mov ebp,esp

    ;; Get the data
    push dword fmtStart
    call printf
    add esp, 4

    push dword int2
    push dword int1
    push dword input
    call scanf
    add esp, 12

        ;; Do calculations
        ;; Add
            xor eax, eax
            mov eax, [int1]
            add eax, [int2]
            mov [sum], eax
        push dword sum
        push dword fmtTest
        call printf
        add esp, 24

    mov esp, ebp    ; take down stack frame
    pop ebp         ; same as "leave" op

    mov eax,0       ; normal, no error, return value
    ret                 ; return

I get: 我得到:

Enter two numbers in format '# #' 输入格式为“##”的两个数字

2 3 2 3

sum: 4247592 总和:4247592

which isn't what I get when I add 2 and 3 with my calculator, maybe that's just me though. 当我在计算器上加上2和3时,这不是我得到的,也许只是我一个人。

my understanding of the code is as follows: the data section declares variables that are initialized to stuff, in this case my formatted strings; 我对代码的理解如下:数据部分声明了初始化为填充的变量,在这种情况下为格式化的字符串; the bss section is for uninitialized variables, in this case my input vars and the sum var; bss部分用于未初始化的变量,在这种情况下,我的输入变量是var和sum变量; the text section is where the code goes; 文本部分是代码所在的位置; I declare main as the entry point for gcc; 我声明main为gcc的入口点; I prompt the user for two numbers; 我提示用户输入两个数字。 I zero out eax with the xor; 我把异或归零。 move the value of int1 to eax; 将int1的值移至eax; add the value of int2 to eax; 将int2的值添加到eax; move what's in eax to be the value of sum; 将eax中的内容移动为sum的值; push it onto the stack with the formatted string; 用格式化的字符串将其压入堆栈; call printf to display stuff; 调用printf显示内容; end the program. 结束程序。

--EDIT-- - 编辑 -

To be clear, either add isn't working or mov isn't working. 需要明确的是,要么add不起作用,要么mov不起作用。 It seems like add should be working so I'm assuming it's mov. 看来add应该能正常工作,所以我假设它是移动的。 I don't understand what about mov [var], register would be wrong but obviously something isn't right! 我不了解mov [var], register会出错,但显然有些错误!

Here's the problem: 这是问题所在:

    push dword sum
    push dword fmtTest
    call printf

printf , unlike scanf , takes its arguments (after the format) by value, while in your code sum is the address of the memory location. scanf不同, printf通过值取其参数(在格式之后),而在代码中sum是内存位置的地址 Just do: 做就是了:

    push [sum]
    push fmtTest
    call printf

(incidentally, the xor eax,eax before the mov eax,[int1] is useless, since you are immediately rewriting the content of the register) (顺便说一句, xor eax,eax mov eax,[int1]之前的mov eax,[int1]没用,因为您将立即重写寄存器的内容)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM