【发布时间】:2021-02-14 00:22:13
【问题描述】:
该程序的目的是从用户那里获取 3 个参数和一个附加参数并求其总和。
我让它工作,以便它找到 3 个参数的总和并打印出总和。
如何打印不带参数的字符串以及如何获取用户的输入并打印该输入?
includelib legacy_stdio_definitions.lib
extrn printf:near, scanf:near
.data ; Data section
istr db 'Please enter an integer: '
stri byte '%lld', 0AH, 00
ostr byte 'The sum of proc. and user inputs (%lld, %lld, %lld, %lld): %lld', 0AH, 00
.code ; Code section
public use_scanf ; int use_scanf(long long a, long long b, long long c)
use_scanf: ; {
mov rax, 0 ; sum = 0;
add rax, rcx ; sum += a;
add rax, rdx ; sum += b;
add rax, r8 ; sum += c;
; printf('Please enter an integer');
; scanf();
; printf('%lld', &inp_int);
mov r9, 3 ; temp inp_int
add rax, r9 ; sum += inp_int;
push rbx ; save reg rbx to make space
mov rbx, rax ; save sum in rbx
push rax ; sum.push
push rax ; sum.push
push r9 ; usrinput.push
sub rsp, 32 ; allocate shadow space
mov r9, r8 ; arg3 = c;
mov r8, rdx ; arg2 = b;
mov rdx, rcx ; arg1 = a
lea rcx, ostr ; arg0 = ostr;
call printf ; printf(ostr);
add rsp, 56 ; clear shadow space and vars from stack
mov rax, rbx ; retVal = sum
pop rbx ; return rbx to previous value
ret ; return retVal}
编辑:控制台输出
Please enter an integer: 2
use_scanf_spill4 equ 12 * 8 ; The slots above return address that are
The sum of proc. and user inputs (1, 2, 3, 1002523588520): 1002523588526
use_scanf(1,2,3) = 73 ERROR: should be 7018134685179969542
Please enter an integer: 2
The sum of proc. and user inputs (-3, 2, -2, 1002523588520): 1002523588517
use_scanf(-3,2,-2) = 75 ERROR: should be 7018134685179969533
Please enter an integer: 3
The sum of proc. and user inputs (4, 3, -4, 1002523588520): 1002523588523
use_scanf(4,3,-4) = 74 ERROR: should be 7018134685179969539
Please enter an integer: -3
The sum of proc. and user inputs (-3, -3, -4, 1002523588520): 1002523588510
use_scanf(-3,-3,-4) = 76 ERROR: should be 7018134685179969526
下面是用来测试的cpp文件。
void check(const char *s, _int64 v, _int64 expected) {
std::cout << s << " = " << v;
if (v == expected) {
std::cout << " OK";
}
else {
std::cout << " ERROR: should be " << expected;
}
std::cout << "\n";
}
_int64 sum_scanf;
_int64 sum_check;
sum_scanf = use_scanf(1, 2, 3);
sum_check = 1 + 2 + 3 + inp_int;
check("use_scanf(1,2,3)", sum_scanf, sum_check);
std::cout << "\n";
sum_scanf = use_scanf(-3, 2, -2);
sum_check = -3 + 2 - 2 + inp_int;
check("use_scanf(-3,2,-2)", sum_scanf, sum_check);
std::cout << "\n";
sum_scanf = use_scanf(4, 3, -4);
sum_check = 4 + 3 - 4 + inp_int;
check("use_scanf(4,3,-4)", sum_scanf, sum_check);
std::cout << "\n";
sum_scanf = use_scanf(-3, -3, -4);
sum_check = -3 - 3 - 4 + inp_int;
check("use_scanf(-3,-3,-4)", sum_scanf, sum_check);
【问题讨论】:
标签: windows assembly printf scanf x86-64