#include <stdio.h> #include <string.h> #include <windows.h> #include <stdio.h> int…
- User
#include <stdio.h> #include <string.h> #include <windows.h>
#include <stdio.h>
int my_atoi(const char *str) { int result = 0; int sign = 1; int i = 0;
__asm__ __volatile__ ( "1:\n" // Label for skip_spaces "movb (%[str], %[i]), %%al\n" // Load byte from str[i] into al "cmpb $' ', %%al\n" // Compare al with space "jne 2f\n" // Jump to check_sign if not space "incl %[i]\n" // Increment i "jmp 1b\n" // Jump back to skip_spaces "2:\n" // Label for check_sign "cmpb $'-', %%al\n" // Compare al with '-' "jne 3f\n" // Jump to check_plus if not '-' "movl $-1, %[sign]\n" // Set sign to -1 "incl %[i]\n" // Increment i "jmp 4f\n" // Jump to parse_loop "3:\n" // Label for check_plus "cmpb $'+', %%al\n" // Compare al with '+' "jne 4f\n" // Jump to parse_loop if not '+' "incl %[i]\n" // Increment i "4:\n" // Label for parse_loop "movb (%[str], %[i]), %%al\n" // Load current byte from str[i] "cmpb $0, %%al\n" // Check for end of string "je 5f\n" // Jump to finish if zero "cmpb $'0', %%al\n" // Compare with '0' "jb 5f\n" // Jump to finish if less than '0' "cmpb $'9', %%al\n" // Compare with '9' "ja 5f\n" // Jump to finish if greater than '9' "subb $'0', %%al\n" // Convert char to number "imull $10, %[result]\n" // Multiply result by 10 "addl %%eax, %[result]\n" // Add current digit to result "incl %[i]\n" // Increment i "jmp 4b\n" // Repeat parse_loop "5:\n" // Label for finish "imull %[sign], %[result]\n" // Apply sign to result : [result] "=r" (result), [i] "+r" (i) // Output and inout constraints : [str] "r" (str), [sign] "r" (sign) // Input constraints : "eax" // Clobbered registers ); return result;}
// LCG 伪随机数生成器参数 #define LCG_A 1664525 #define LCG_C 1013904223 #define LCG_M 4294967296 static unsigned int lcg_seed = 1;
// LCG 生成随机数 unsigned int lcg_rand() { lcg_seed = (LCG_A * lcg_seed + LCG_C) % LCG_M; return lcg_seed; }
// 使用 Windows API 获取当前时间并设置种子 void lcg_srand_from_system_time() { SYSTEMTIME time; GetSystemTime(&time); lcg_seed = (unsigned int)(time.wSecond + time.wMilliseconds); }
double simulateAverageSingleThread(int numSimulations, int roleCount, int weaponCount) { lcg_srand_from_system_time(); // 初始化随机种子
double baseRate = 0.006; const int pityStartRole = 73; const int hardPityRole = 90; const int pityStartWeapon = 63; const int hardPityWeapon = 80; double rateIncrease = 0.062; int totalPulls = 0; for (int i = 0; i < numSimulations; ++i) { int pulls = 0; int guaranteedRole = 0, guaranteedWeapon = 0; int roleObtained = 0, weaponObtained = 0; int rolePullCount = 0, weaponPullCount = 0; // 先抽角色池 while (roleObtained < roleCount) { double pullRate = baseRate; if (rolePullCount >= pityStartRole) { pullRate += (rolePullCount - pityStartRole) * rateIncrease; } int gotGold = (rolePullCount >= hardPityRole - 1) || ((double)lcg_rand() / LCG_M < pullRate); if (gotGold) { rolePullCount = 0; double guaranteedRate = 0.5; if (guaranteedRole || ((double)lcg_rand() / LCG_M < guaranteedRate)) { guaranteedRole = 0; roleObtained++; } else { guaranteedRole = 1; } } else { rolePullCount++; } pulls++; } // 再抽武器池 while (weaponObtained < weaponCount) { double pullRate = baseRate; if (weaponPullCount >= pityStartWeapon) { pullRate += (weaponPullCount - pityStartWeapon) * rateIncrease; } int gotGold = (weaponPullCount >= hardPityWeapon - 1) || ((double)lcg_rand() / LCG_M < pullRate); if (gotGold) { weaponPullCount = 0; double guaranteedRate = 0.75; if (guaranteedWeapon || ((double)lcg_rand() / LCG_M < guaranteedRate)) { guaranteedWeapon = 0; weaponObtained++; } else { guaranteedWeapon = 1; } } else { weaponPullCount++; } pulls++; } totalPulls += pulls; } return (double)totalPulls / numSimulations;}
double simulateWithItemsSingleThread(int numSimulations, int roleCount, int weaponCount, int items, int initialRolePulls, int initialWeaponPulls, int majorPityRole, int majorPityWeapon) { lcg_srand_from_system_time(); // 初始化随机种子
double baseRate = 0.006; const int pityStartRole = 73; const int hardPityRole = 90; const int pityStartWeapon = 63; const int hardPityWeapon = 80; double rateIncrease = 0.062; int successfulSimulations = 0; for (int i = 0; i < numSimulations; ++i) { int pulls = 0; int guaranteedRole = 0, guaranteedWeapon = 0; int currentMajorPityRole = majorPityRole; int currentMajorPityWeapon = majorPityWeapon; int roleObtained = 0, weaponObtained = 0; int rolePullCount = initialRolePulls, weaponPullCount = initialWeaponPulls; // 先抽角色池 while (roleObtained < roleCount && pulls < items) { double pullRate = baseRate; if (rolePullCount >= pityStartRole) { pullRate += (rolePullCount - pityStartRole) * rateIncrease; } int gotGold = (rolePullCount >= hardPityRole - 1) || ((double)lcg_rand() / LCG_M < pullRate); if (gotGold) { rolePullCount = 0; double guaranteedRate = 0.5; if (guaranteedRole || currentMajorPityRole || ((double)lcg_rand() / LCG_M < guaranteedRate)) { guaranteedRole = 0; currentMajorPityRole = 0; roleObtained++; } else { guaranteedRole = 1; } } else { rolePullCount++; } pulls++; } // 再抽武器池 while (weaponObtained < weaponCount && pulls < items) { double pullRate = baseRate; if (weaponPullCount >= pityStartWeapon) { pullRate += (weaponPullCount - pityStartWeapon) * rateIncrease; } int gotGold = (weaponPullCount >= hardPityWeapon - 1) || ((double)lcg_rand() / LCG_M < pullRate); if (gotGold) { weaponPullCount = 0; double guaranteedRate = 0.75; if (guaranteedWeapon || currentMajorPityWeapon || ((double)lcg_rand() / LCG_M < guaranteedRate)) { guaranteedWeapon = 0; currentMajorPityWeapon = 0; weaponObtained++; } else { guaranteedWeapon = 1; } } else { weaponPullCount++; } pulls++; } if (roleObtained >= roleCount && weaponObtained >= weaponCount) { successfulSimulations++; } } return (double)successfulSimulations / numSimulations * 100.0;}
int main(int argc, char* argv[]) { if (argc < 2) { fprintf(stderr, "Usage: %s ...\n", argv[0]); return 1; }
if (strcmp(argv[1], "-average") == 0) { if (argc < 4) { fprintf(stderr, "Usage: %s -average <roleCount> <weaponCount>\n", argv[0]); return 1; } int roleCount = my_atoi(argv[2]); int weaponCount = my_atoi(argv[3]); double averagePulls = simulateAverageSingleThread(50000, roleCount, weaponCount); double starJades = averagePulls * 160.0; printf("%.1f\n约合星琼数 %.1f\n", averagePulls, starJades); } else if (strcmp(argv[1], "-simulate") == 0) { if (argc < 9) { fprintf(stderr, "Usage: %s -simulate <roleCount> <weaponCount> <items> <initialRolePulls> <initialWeaponPulls> <majorPityRole> <majorPityWeapon>\n", argv[0]); return 1; } int roleCount = my_atoi(argv[2]); int weaponCount = my_atoi(argv[3]); int items = my_atoi(argv[4]); int initialRolePulls = my_atoi(argv[5]); int initialWeaponPulls = my_atoi(argv[6]); int majorPityRole = my_atoi(argv[7]); int majorPityWeapon = my_atoi(argv[8]); double successRate = simulateWithItemsSingleThread(50000, roleCount, weaponCount, items, initialRolePulls, initialWeaponPulls, majorPityRole, majorPityWeapon); printf("成功率 %.3f%%\n", successRate); } else { fprintf(stderr, "Invalid function specified.\n"); return 1; } return 0;}32 C:\Users\surface\AppData\Local\Temp\ccAUcsUL.s Error: `(%rcx,%edx)' is not a valid base/index expression
- Assistant
The error message you're encountering indicates a problem with an assembly instruction in your code:
(%rcx,%edx)is not valid for addressing within the inline assembly inmy_atoifunction. This is likely due to differences between register sizes or constraints not matching the platform's architecture. Here are a few points to improve and potentially fix this:Key Aspects to Consider:
-
Register Sizes: Ensure you are using the correct register sizes for your platform. The use of registers like
%eax,%rcx, and%rdximplies different operand sizes. On x86-64, you should be ensuring everything aligns to the 64-bit registers unless explicitly working with smaller segments. -
Constraints: Revisit the constraints in your inline assembly, making sure the input/output operands and clobbers match up correctly to your inline assembler requirements.
-
Register Access: Inline assembly may be very sensitive to how arrays and pointers are accessed. This includes addressing modes, which should align with the platform conventions and C/C++ rules.
Here's an edited version of your
my_atoifunction that eliminates inline assembly altogether for readability and maintainability. It uses straightforward C for conversion:C Implementation of
my_atoi:int my_atoi(const char *str) { int result = 0; int sign = 1; int i = 0; // Skip white spaces while (str[i] == ' ') i++; // Check for sign if (str[i] == '-') { sign = -1; i++; } else if (str[i] == '+') { i++; } // Convert the number while (str[i] >= '0' && str[i] <= '9') { result = result * 10 + (str[i] - '0'); i++; } return result * sign; }Notes:
-
Assembly to High-Level Code: Replacing assembly code with high-level C constructs can make your code portable and usually more readable unless there’s a specific performance or hardware-related need for inline assembly.
-
Platform-Specific Code: Inline assembly is useful but often avoided unless necessary, as it ties your code to a specific architecture. Consider using compiler intrinsics or optimized libraries where possible.
Ensure that your build process targets the correct architecture, and if using features specific to a platform (like Windows APIs), ensure header and library paths are correct based on your build settings.
-
- User
说中文
- Assistant
好的,我们来看看您遇到的问题以及如何解决。
您遇到的错误与内联汇编有关,
(%rcx,%edx)不符合有效的基/索引表达式。这可能是因为寄存器大小不匹配或在不同平台上的约束使用不当导致的。解决建议:
-
寄存器大小:确保您在编写内联汇编时使用了正确的寄存器和正确的大小。x86-64平台通常使用64位寄存器,例如
%rax,%rcx等。 -
约束:检查内联汇编中的输入/输出约束,确保它们与平台的要求一致。
-
汇编到C转换:通常,建议用标准C代码替代内联汇编,这样不仅可读性更高,而且代码可移植性更好。除非在性能上有特殊需求,否则一般不建议在C代码中直接嵌入汇编。
用C语言实现
my_atoi函数:以下是一个用C语言实现的
my_atoi函数示例,避免使用内联汇编:int my_atoi(const char *str) { int result = 0; int sign = 1; int i = 0; // 跳过空白字符 while (str[i] == ' ') i++; // 检查符号 if (str[i] == '-') { sign = -1; i++; } else if (str[i] == '+') { i++; } // 转换字符串到整数 while (str[i] >= '0' && str[i] <= '9') { result = result * 10 + (str[i] - '0'); i++; } return result * sign; }备注:
-
高层次代码替代汇编:用标准C代码代替汇编代码可以提高代码的可维护性和可移植性。
-
平台相关代码:如果需要使用特定平台的特性,例如Windows API,确保编译器设置和库路径正确。
如有其他问题或需要进一步帮助,请随时告知。
-
- User
怎么把程序压缩
- Assistant
如果您想将程序文件进行压缩,可以使用各种压缩工具来实现。这些工具可以将您的可执行文件、源代码或其他资源文件压缩成更小的体积。以下是一些常见的压缩方法和工具:
使用压缩工具
-
ZIP 文件:
- 在 Windows 上,您可以右键点击一个文件或文件夹,然后选择“发送到” -> “压缩 (zip) 文件夹”。
- 在 Linux 或 Mac 上,您可以使用
zip命令:zip -r 压缩后的文件名.zip 要压缩的目录或文件
-
7-Zip(适用于 Windows):
- 下载安装 7-Zip。
- 右键点击文件或文件夹,选择“7-Zip” -> “添加到压缩文件…”,选择压缩格式为
.7z。
-
Tar 和 Gzip(主要用于 Linux 和 Mac):
- 使用
tar与gzip可以打包并压缩文件:tar -czvf 压缩后的文件名.tar.gz 要压缩的目录或文件
- 使用
使用程序压缩工具
如果您希望压缩可执行文件(例如,将 Windows 可执行文件压缩为较小的体积),可以使用一些专门的工具,比如:
- UPX(Ultimate Packer for eXecutables):
- 这是一个开源工具,可以用来压缩可执行文件。
- 下载并安装 UPX,然后在命令行中运行:
upx -9 可执行文件名.exe -9表示最高压缩率。
注意事项
- 压缩后的性能:部分工具(如 UPX)可能会影响程序的启动速度,因为压缩文件在运行时需要解压。
- 兼容性:确保您使用的压缩格式在您的目标环境中可用和可解压。
- 安全性:如果需要分发压缩后的可执行文件,确保使用的压缩工具和版本的安全性,防止恶意软件。
根据您的实际需求,选择合适的工具和方法进行压缩。
-