【问题标题】:Max in an array of 6 numbers// It does not display the max6 个数字的数组中的最大值// 不显示最大值
【发布时间】:2016-12-07 01:48:59
【问题描述】:

我需要这个数组的最大值和它的位置。在我看来,现在它应该显示数组的最大值,但它没有。 我主要需要帮助和获取最大值的循环,而在另一个我现在尚未创建的获取最大值位置的循环中,最终程序应该显示最大值标记和模块的编号。

 #define _CRT_SECURE_NO_WARNINGS
 #include "stdafx.h"
 #include <stdio.h>
 #include <stdlib.h>
int main(void){
char get1[] = "Enter the mark for module 1:";
char get2[] = "Enter the mark for module 2:";
char get3[] = "Enter the mark for module 3:";
char get4[] = "Enter the mark for module 4:";
char get5[] = "Enter the mark for module 5:";
char get6[] = "Enter the mark for module 6:";
char out1[] = "Best mark is ";
char out2[] = "for module";
char format[] = "%d"; // format string for the scanf function
int x;
int myarray[5];
int max = 0;
int module = 0;// declare variables in C
_asm {
    mov ebx, 0
    mov eax, x
    mov myarray[ebx], eax
        while1:lea eax, get1; 
        push eax
        call printf
        add esp, 4

        lea eax, x; 
        push eax
        lea eax, format
        push eax
        call scanf
        add esp, 8

        mov eax, x
        cmp eax, 0
        jl while1

        mov eax, x
        cmp eax, 100
        jg while1



        mov ebx, 1
        mov eax, x
        mov myarray[ebx], eax


        while2 : lea eax, get2; 
        push eax
        call printf
        add esp, 4

        lea eax, x; 
        push eax
        lea eax, format
        push eax
        call scanf
        add esp, 8

        mov eax, x
        cmp eax, 0
        jl while2
        mov eax, x
        cmp eax, 100
        jg while2



        mov ebx, 2
        mov eax, x
        mov myarray[ebx], eax

        while3 : lea eax, get3; 
        push eax
        call printf
        add esp, 4

        lea eax, x; 
        push eax
        lea eax, format
        push eax
        call scanf
        add esp, 8

        mov eax, x
        cmp eax, 0
        jl while3
        mov eax, x
        cmp eax, 100
        jg while3



        mov ebx, 3
        mov eax, x
        mov myarray[ebx], eax

        while4 : lea eax, get4; 
        push eax
        call printf
        add esp, 4

        lea eax, x; 
        push eax
        lea eax, format
        push eax
        call scanf
        add esp, 8

        mov eax, x
        cmp eax, 0
        jl while4
        mov eax, x
        cmp eax, 100
        jg while4



        mov ebx, 4
        mov eax, x
        mov myarray[ebx], eax

        while5 : lea eax, get5; // ask for the mark
        push eax
        call printf
        add esp, 4

        lea eax, x; 
        push eax
        lea eax, format
        push eax
        call scanf
        add esp, 8

        mov eax, x
        cmp eax, 0
        jl while5
        mov eax, x
        cmp eax, 100
        jg while5



        mov ebx, 5
        mov eax, x
        mov myarray[ebx], eax

        while6 : lea eax, get6; 
        push eax
        call printf
        add esp, 4

        lea eax, x; //read it in
        push eax
        lea eax, format
        push eax
        call scanf
        add esp, 8

        mov eax, x
        cmp eax, 0
        jl while6
        mov eax, x
        cmp eax, 100
        jg while6

            MOV ebx, 0
            MOV eax, myarray[ebx]
            MOV ecx, 5

            LAB1:CMP myarray[ebx], eax
            JAE PASS
            MOV eax, myarray[ebx]

            PASS : inc ebx
            dec ecx
            cmp ecx, 0
            jg LAB1
            MOV max, eax


            push max
            lea eax, out1
            push eax
            call printf
            add esp, 8

【问题讨论】:

  • 注释您的代码,尤其是如果您希望其他人提供帮助。另外,学习使用调试器,这样您就可以修复自己的错误。
  • 这不是minimal reproducible example(太长了,没有清楚地描述它的作用以及它应该如何工作与它实际作用以及你在调试器中看到的内容)。见How to Ask

标签: assembly x86


【解决方案1】:
int myarray[5];

这定义了一个包含 6 个 dwords 的数组。
因为您似乎不了解像 mov myarray[ebx], eax 这样的指令的真正作用,所以您对数组的所有分配都是重叠的。 [ebx] 部分实际上是数组中的偏移量。 它不是高级语言中的索引。然后你需要像这样缩放:

mov myarray[ebx*4], eax

LAB1:CMP myarray[ebx], eax
     JAE PASS
     MOV eax, myarray[ebx]

此代码确实与您想要的相反。如果数组元素小于EAX 寄存器,则将其放入EAX 寄存器中。这样EAX 寄存器就会越来越小。稍后您会将其显示为 最大值,而实际上(如果其余代码正确)它将是 最小值


为什么要输入6个数字?

  • 第一个数组元素由变量x填充
  • 使用第一个输入填充第二个数组元素
  • 使用第二个输入填充第三个数组元素
  • 使用第三个输入填充第 4 个数组元素
  • 使用第 4 个输入填充第 5 个数组元素
  • 使用第 5 个输入填充第 6 个数组元素

第 6 个输入无处可去!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-30
    • 2021-01-27
    • 2023-03-10
    • 2021-01-13
    • 2016-09-10
    • 2010-09-20
    • 2017-08-09
    • 1970-01-01
    相关资源
    最近更新 更多