如何使用scanf函数在NASM? [英] How To Use Scanf In NASM?

查看:284
本文介绍了如何使用scanf函数在NASM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白这一点。我显然确实的新议会,而我试图找出如何使用scanf函数来获取用户输入。

I can't figure this out. I'm obviously really new to Assembly, and I'm trying to figure out how to use Scanf to get user input.

我知道用printf所有我所要做的就是推动我想在屏幕上书写进栈这样的数据:

I know to use Printf all I have to do is push the data I want to write on the screen into the stack like this:

global _main
extern _printf
extern _scanf

section .data
msg db "Hi", 0

section .text
_main:
  push ebp
  mov ebp, esp  

  push msg
  call _printf

  mov esp, ebp
  pop ebp
ret

但我无法弄清楚如何使用scanf函数。可有人请给我一个最简单的源$ C ​​$ C,可用于scanf函数?我真的只是想放什么用户键入

But I can't figure out how to use Scanf. Can someone please just give me the simplest possible source code you can for Scanf? I really just want to put what the user types in.

我不习惯32bit的大会。我只用过16位,我在16位知道(DOS),你可以这样做:

I'm not used to 32bit Assembly. I've only ever used 16bit, and I know in 16bit (DOS) you can just do this:

mov ah, 3fh
mov dx, input
int 21h

input rb 100d

不管你键入将放置在地址输入。

And whatever you type in will the placed at the address of "input."

请多多包涵。我试图弄清楚这一点。

Please bear with me. I'm trying to figure this out.

推荐答案

我发现这个在NASM.PDF编程

; add1.asm
SECTION .data
    message1: db "Enter the first number: ", 0
    message2: db "Enter the second number: ", 0
    formatin: db "%d", 0
    formatout: db "%d", 10, 0 ; newline, nul terminator
    integer1: times 4 db 0 ; 32-bits integer = 4 bytes
    integer2: times 4 db 0 ;
SECTION .text
   global _main 
   extern _scanf 
   extern _printf     

_main:

   push ebx ; save registers
   push ecx
   push message1
   call printf

   add esp, 4 ; remove parameters
   push integer1 ; address of integer1 (second parameter)
   push formatin ; arguments are right to left (first parameter)
   call scanf

   add esp, 8 ; remove parameters
   push message2
   call printf

   add esp, 4 ; remove parameters
   push integer2 ; address of integer2
   push formatin ; arguments are right to left
   call scanf

   add esp, 8 ; remove parameters

   mov ebx, dword [integer1]
   mov ecx, dword [integer2]
   add ebx, ecx ; add the values          ; the addition
   push ebx
   push formatout
   call printf                            ; call printf to display the sum
   add esp, 8                             ; remove parameters
   pop ecx
   pop ebx ; restore registers in reverse order
   mov eax, 0 ; no error
   ret

这是这个C函数的ASM版本:

Which is the asm version of this C function:

#include <stdio.h>
int main(int argc, char *argv[])
{
    int integer1, integer2;
    printf("Enter the first number: ");
    scanf("%d", &integer1);
    printf("Enter the second number: ");
    scanf("%d", &integer2);
    printf("%d\n", integer1+integer2);
    return 0;
}

这篇关于如何使用scanf函数在NASM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆