gpt4 book ai didi

c - 使用 C 和内联汇编器查找数组中最大的 float

转载 作者:行者123 更新时间:2023-12-02 03:41:53 25 4
gpt4 key购买 nike

我对 C 和汇编程序很陌生(真的是所有编程),这确实困扰了我一两天。

这是我的作业(我已经完成了 4 项。这是我遇到问题的额外学分。):

对于每个问题,您必须编写一个包含三个部分的 C(而不是 C++)程序:

A.一些用于读取输入的 C 代码(使用 scanf)。

B.一段用于进行计算的内联汇编器。

C.一些用于编写输出的 C 代码(使用 printf)。

maxi.c :读取计数 n,然后将 n 个整数的列表读入数组(使用 malloc 分配),然后使用汇编器查找所有整数中最大的整数,然后输出它。您将需要像 jg 这样的条件跳转操作码。要使用这样的操作码,通常首先使用 dec 或 sub 等操作码,它会设置标志寄存器中的位。然后,例如,如果前一个操作的结果大于零,您将使用 jg 跳转到某处(例如循环顶部)。您还可以使用 cmp 操作码设置标志寄存器。您还需要使用基本位移模式在数组中进行访问:mov eax,[ebx+ecx*4]。

额外积分:5. maxf.c :与上面相同,但使用 float 而不是整数。

这就是我现在所拥有的。当我输入列表时,它输出列表中的第一个数字,无论它是否是最大的。

// maxi.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "malloc.h"

int n; // length of list
float *nums; // the list
int max; // the result

int main()
{
int i;
float arr[10];
printf("How many integers? ");
scanf_s("%d", &n);
nums = (float*)malloc(n * sizeof(float));
for (i = 0; i<n; i++)
{
printf("Enter next number: ");
scanf_s("%d", &arr[i]);
}

__asm
{
mov eax, n; // A is the count
dec eax; // A becomes the end of the list
mov ebx, nums; // B is the beginning of the list
mov ecx, arr; // C is the current largest integer

top:
cmp eax, 0;
jl done;

mov edx, [ebx + eax * 4];
cmp edx, ecx;
jg Change;
jmp Increment;

Increment:
//mov eax,[ebx+ecx*4];
dec eax;
jmp top;

Change:
mov ecx, edx;
jmp Increment;
done:
mov max, ecx;
}
printf("The largest integer in your list is: %d\n", max);
return 0;
}

最佳答案

我认为您混淆了两个变量 arr 和 nums。您将 nums 分配到正确的大小,但将数字读入 arr 中,arr 已预先分配以容纳 10 个数字。首先解决这个问题,然后看看组装的东西。

关于c - 使用 C 和内联汇编器查找数组中最大的 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34933694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com