【问题标题】:Output several values from array to MessageBox line by line将数组中的多个值逐行输出到 MessageBox
【发布时间】:2020-03-24 13:52:03
【问题描述】:

我有一个程序可以使用循环以不同的值解决数学问题 5 次。它将结果存储在result 数组中,所以最后我需要从新行开始显示这些值:

result[0]
result[4]
result[8]
result[12]
result[16]

但我似乎不明白如何在我的代码中实现这一点,我尝试的所有方法都不起作用。现在它只显示 1 个结果。我知道,一个新行的开始方式是,10,但我就是不知道该把它放在哪里。

.386
.model flat,stdcall
option casemap:none

include \masm32\include\masm32rt.inc

.data
titletext db 'Лаб 5', 0
AnswerTxt db 'The answer is '
AnswerNum db 11 dup(0)
num_a dd 5,-5,-25,25,-5
num_b dd 5,32,3,-5,6
num_c dd 10,2,8,12,4
bracket dd ?
result dd ?,?,?,?,?

.code
start:
    mov esi, 5
    mov edi, 0
    .Repeat
        mov eax, -25
        mov ebx, num_a[edi]
        cdq
        idiv ebx
        add eax, num_c[edi]
        mov bracket, eax
        mov eax, num_b[edi]
        mov ebx, num_a[edi]
        cdq
        imul ebx
        sub bracket, eax
        mov eax, num_c[edi]
        mov ebx, num_b[edi]
        cdq
        imul ebx
        mov ebx, 2
        cdq
        idiv ebx
        add eax, 1
        mov ebx, bracket
        cdq
        imul ebx
        mov result[edi], eax

        mov ebx, 2
        cdq
        idiv ebx

        .IF edx == 0
            mov eax, result[edi]
            mov ebx, 2
            cdq
            idiv ebx
        .ELSE
            mov eax, result[edi]
            mov ebx, 5
            cdq
            imul ebx
        .ENDIF

        mov result[edi], eax
        add edi, 4
        dec esi
        .Until Zero?

        push offset AnswerNum
        push result[12]
        call dwtoa
        push 0
        push offset titletext
        push offset AnswerTxt
        push 0
        call MessageBox
        call ExitProcess
end start

【问题讨论】:

  • 为什么不直接使用wsprintf或者其他类似的函数,直接在格式字符串中放置换行符呢?
  • @Michael 如果您有时间和机会,您能否提供一些示例或一些关于如何在我的代码中调整它的信息?我刚开始接触 asm 和 MessageBox 输出,AnswerNum 来自一个更简单的加法计算器示例。我什至不明白为什么我们需要保留这个空间以便稍后在其中放置一个 ASCII 整数。
  • MASM32 附带大量示例,其中有几个使用wsprintf。例如,examples\exampl02\direxamples\exampl03\lcd
  • @Michael 谢谢!会看的!

标签: assembly x86 masm masm32


【解决方案1】:
.386
.model flat,stdcall
option casemap:none

include \masm32\include\masm32rt.inc

.data
titletext db  'Лаб 5',0
frmt db  '1 = %d',10
     db  '2 = %d',10
     db  '3 = %d',10
     db  '4 = %d',10
     db  '5 = %d',0
buff db  256 dup (0)
result dd ?,?,?,?,?

在填充result[0],result[4],result[8],result[12],result[16]数组之后:

        invoke  wsprintf,addr buff,addr frmt,result[0],result[4],result[8],result[12],result[16]
        invoke  MessageBox,0,addr buff,addr titletext,MB_OK
        invoke  ExitProcess,0
end start

你也可以使用 crt_sprintf 函数,使用浮点数更加灵活。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 2017-11-12
    • 2018-11-05
    • 1970-01-01
    • 2016-07-27
    • 2017-07-22
    • 1970-01-01
    相关资源
    最近更新 更多