我的汇编代码没有运行,我不知道为什么已经设置了 Visual Studio 来运行 x86 汇编

问题描述 投票:0回答:1
我的代码没有运行,我失去了理智,不知道发生了什么,视觉工作室说的错误位于主进程“mov edx,OFFSET msg1”下的第一行,至少在它现在看起来工作正常之前是一个主(void),但是代码没有给我正确的输出有人可以帮忙吗?

INCLUDE irvine32.inc includelib irvine32.lib .data arabic DWORD 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 roman BYTE "M\0\0", "CM\0", "D\0\0", "CD\0", "C\0\0", "XC\0", "L\0\0", "XL\0", "X\0\0", "IX\0", "V\0\0", "IV\0", "I\0\0" N DWORD 0 msg1 BYTE 'Enter arabic number:', 0 msg2 BYTE '\nRoman number:', 0 .code main PROC ; Print msg1 mov edx, OFFSET msg1 call WriteString ; Read input into N call ReadInt mov DWORD PTR [N], eax ; Print msg2 mov edx, OFFSET msg2 call WriteString mov esi, 0 convert_loop: cmp esi, 12 jge exit_loop ; Compare N with arabic[i] mov eax, [arabic + esi * 4] cmp N, eax jl increment_index ; Print Roman numeral and update N movzx eax, [esi * 2 + roman] call WriteChar sub N, eax jmp convert_loop increment_index: add esi, 1 jmp convert_loop exit_loop: invoke ExitProcess, 0 main ENDP END main
我被告知我的数据部分不正确,并将其修复为现在不确定是否有帮助,我将移动 edx 切换为 PUSH,但这使情况变得更糟,所以我将其切换回来,不确定还能做什么。
编辑:有点提到它,但它运行良好,没有 main(void) 错误,但如果有可能告诉我为什么会发生那就太好了,现在唯一的问题是它没有给我正确的罗马数字

assembly x86 masm
1个回答
0
投票
roman BYTE "M\0\0", "CM\0", "D\0\0", "CD\0", "C\0\0", "XC\0", "L\0\0", "XL\0", "X\0\0", "IX\0", "V\0\0", "IV\0", "I\0\0"

这是一个包含

13 个以零结尾的字符串的数组,每个字符串包含一个或两个可显示字符。

专注于行动所在:

mov esi, 0 convert_loop: cmp esi, 12 jge exit_loop ; Compare N with arabic[i] mov eax, [arabic + esi * 4] cmp N, eax jl increment_index ; Print Roman numeral and update N movzx eax, [esi * 2 + roman] call WriteChar sub N, eax jmp convert_loop increment_index: add esi, 1 jmp convert_loop

    您没有使用第 13 个数组元素。
  • 您对
  • roman 数组的寻址不同步,因为时间为 2 而不是时间 3。请参阅 vitsoft 的评论
  • 您读写了一个字符,因此留下了一半的 2 字符字符串
  • 您从
  • N 中减去一个虚假值。您减去的内容必须来自阿拉伯数组。
  • 即使打印罗马字符,您仍然需要增加索引。
接下来是快速重写,您应该注意我已将循环条件放在底部,从而去掉了无条件

jmp

xor esi, esi convert_loop: mov ebx, [arabic + esi * 4] ; Compare N with arabic[i] cmp N, ebx jb increment_index lea edx, [roman + esi + esi * 2] ; Print Roman numeral and update N call WriteString ; It's a string, not just a char sub N, ebx ; Subtract the arabic value jz early_out ; An optional early out... increment_index: inc esi cmp esi, 13 ; Valid indexes are [0,12] jb convert_loop early_exit:
    
© www.soinside.com 2019 - 2024. All rights reserved.