在汇编中手动编程cin 和cout 几乎是不可能的。但是您可以使用 C 库的函数 printf 和 scanf,它是 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++。