Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Assembly 装配输入输出_Assembly_Io_Fasm - Fatal编程技术网

Assembly 装配输入输出

Assembly 装配输入输出,assembly,io,fasm,Assembly,Io,Fasm,我有两个程序:一个做输入输出(C++),另一个计算公式(汇编)。他们互相合作 该程序不进行输入输出: #include <iostream> using namespace std; extern "C" int calc(int a, int b, int c, int d); int main() { int a; cout<<"a:"<<endl; cin>>a; cout<<"b:"<

我有两个程序:一个做输入输出(C++),另一个计算公式(汇编)。他们互相合作

该程序不进行输入输出:

#include <iostream>

using namespace std;

extern "C" int calc(int a, int b, int c, int d);

int main()
{
    int a;
    cout<<"a:"<<endl;
    cin>>a;
    cout<<"b:"<<endl;
    int b;
    cin>>b;
    cout<<"c:"<<endl;
    int c;
    cin>>c;

    cout<<"d:"<<endl;
    int d;
    cin>>d;

    int calculation = calc(a,b,c,d);
    cout << "5*a-c*d+7*b-2=" <<calculation<< endl;
    return 0;
}
<>你能帮我在汇编中做输入输出(没有C++程序)吗?在这个例子中,我不知道该怎么做。
感谢您的帮助。

您可以在汇编代码中使用C库(libc)中的函数。如何调用C函数的详细信息取决于所使用的汇编器。还有哪些寄存器和/或堆栈用于函数参数,以及顺序取决于可执行目标(32位或64位)。例如,在64位Linux上调用scanf函数的yasm汇编程序语法可以是:

segment .data
format:  db  "%d"  ;format string used in scanf("%d", &var)
var: dd 123    ;variable where we want to read input to (initialized to a random number 123)
segment .text
global main
extern scanf
main:
lea rax, [var]
lea edi, [format]
xor eax, eax
call  scanf
要生成可执行文件,请执行以下操作:

  • yasm-f elf64使用clib.s
  • gcc使用clib.o-o使用clib

我建议使用
gcc
而不是
ld
,因为默认情况下,gcc链接C库。

在汇编中手动编程
cin
cout
几乎是不可能的。但是您可以使用C库的函数
printf
scanf
,C库是C++标准的子集:

format ELF
public main
extrn printf
extrn scanf
extrn fflush

section '.text' executable

calc:
label .a dword at ebp+8                 ; Labels with dot are local labels
label .b dword at ebp+12
label .c dword at ebp+16
label .d dword at ebp+20

    push ebp
    mov ebp, esp

    mov eax, [.a]
    mov ebx, 5
    mul ebx
    mov ecx, eax
    mov eax, [.c]
    mov ebx, [.d]
    mul ebx
    sub ecx, eax
    mov eax, [.b]
    mov ebx, 7
    mul ebx
    add eax, ecx
    sub eax, 2

    mov esp, ebp
    pop ebp
    ret

main:
label .calculation dword at ebp-4       ; Labels with dot are local labels
label .a dword at ebp-8
label .b dword at ebp-12
label .c dword at ebp-16
label .d dword at ebp-20

    push ebp
    mov ebp, esp
    sub esp, 20                         ; Space for the local variables

    lea eax, [in_msg]
    push eax
    call printf                         ; printf ("a: ");
    add esp, 4

    lea eax, [.a]                       ; Pointer to .a (effective address of .a)
    push eax
    push scan_fmt
    call scanf                          ; scanf (" %d", &a);
    add esp, 8                          ; Clean up the stack

    lea eax, [in_msg+4]
    push eax
    call printf                         ; printf ("b: ");
    add esp, 4

    lea eax, [.b]                       ; Pointer to .a (effective address of .a)
    push eax
    push scan_fmt
    call scanf                          ; scanf (" %d", &b);
    add esp, 8                          ; Clean up the stack

    lea eax, [in_msg+8]
    push eax
    call printf                         ; printf ("c: ");
    add esp, 4

    lea eax, [.c]                       ; Pointer to .a (effective address of .a)
    push eax
    push scan_fmt
    call scanf                          ; scanf (" %d", &c);
    add esp, 8                          ; Clean up the stack

    lea eax, [in_msg+12]
    push eax
    call printf                         ; printf ("d: ");

    add esp, 4
    lea eax, [.d]                       ; Pointer to .a (effective address of .a)
    push eax
    push scan_fmt
    call scanf                          ; scanf (" %d", &d);
    add esp, 8                          ; Clean up the stack

    push [.d]                           ; Call by value
    push [.c]                           ; Call by value
    push [.b]                           ; Call by value
    push [.a]                           ; Call by value
    call calc                           ; EAX = calc (a,b,c,d)
    add esp, 16                         ; Clean up the stack
    mov [.calculation], eax             ; Store result for later use

    push [.calculation]
    push out_fmt
    call printf                         ; printf("5*a-c*d+7*b-2=%d\n",calculation);
    add esp,8                           ; Clean up the stack

    leave
    ret

section '.data' writeable

    out_fmt     db "5*a-c*d+7*b-2=%d", 10, 0
    scan_fmt    db " %d",0
    in_msg      db "a: ",0,"b: ",0,"c: ",0,"d: ",0
组装、连接并运行它

fasm calc.asm
g++ -m32 -o calc calc.o
./calc

您可以使用
gcc

而不是
g++
。您想使用什么汇编程序(FASM?)?您是如何汇编、编译和链接这两个程序的?@rkhb我使用fasm来编译这两个程序,我使用“g++-m32 calc.cpp calcul.o-o a.out”
fasm calc.asm
g++ -m32 -o calc calc.o
./calc