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) 错误,但如果有可能告诉我为什么会发生那就太好了,现在唯一的问题是它没有给我正确的罗马数字
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
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: