将查询作为问题传递给3个操作数(长度+宽度* 2),无法计算NASM中的周长

时间:2015-07-26 16:10:14

标签: c++ nasm

所以今天我决定再次使用nasm,最近我一直在收集一些知识,因此我决定给自己一些项目,然后才能证明我是好的'用它

我试图用c / c ++中的NASM计算矩形的周长,代码看起来像这样

#include <stdio.h>
#include <stdlib,h>
#include <math.h>

int main()
    {
       int length;
       int breadth;
       int perimeter;

      printf("Enter The Length: \n");
      scanf("%d",&length);
      printf("Enter The Breadth: \n");
      scanf("%d",&breadth);

      perimeter = length + breadth * 2;
     printf("Perimeter is : %d",perimeter);

      return 0;
        system("PAUSE");

    }

哪个应该可以正常工作现在我尝试在NASM中做同样的事情,长度+宽度* 2的部分给出了一些问题,因为它产生了一个相当离谱的答案,而不是原始的源代码看起来像这样

section  .data 

    msg:
    msg1: db 'A program to Calculate The Perimiter of A Rectangle',10,0
    msg2: db '******************************************************',10,0
    ;msg3: db ''Length + Breadth * 2 =  Perimeter'
    msg3: db 'Enter Length :',0
    msg4: db 'Enter Breadth :',0
    msg5: db 'Perimeter is = %d ',0
        formin : db '%d',0
    formout : db '%d',10,0
    num1 : times 4 db 0
    num2 : times 4 db 0

section  .text

    global _main
    extern _scanf
    extern _printf


_main :
    push ebp
    mov esp ,ebp
    push msg1
    call _printf

    push msg2
    call _printf

    push msg3
    call _printf

    mov eax, num1
    push num1
    push formin
    call _scanf

    push msg4
    call _printf

    mov eax, num2
    push num2
    push formin
    call _scanf


    mov eax,dword[num1]
    mov ebx,dword[num2]

    mov ecx,2
    ; Perimeter is always L + B *2

    add eax,ebx

    imul ecx

    push msg5
    call _printf

    ; destroy stack
    pop ebp
    mov esp, ebp

    ret 

请注意周界区域给我带来问题,为什么会这样?

0 个答案:

没有答案