I am quite new to C and assembler (all programming, really), and this has really been bothering me for a day or two.
This is my assignment (I've already done 4. It's the extra credit I'm having a problem with.):
For each of these questions, you must write a C (not C++) program that contains three parts:
A. Some C code to read inputs (using scanf).
B. A segment of inline-assembler to do the computation.
C. Some C code to write the output (using printf).
- maxi.c : reads a count n, then reads a list of of n integers into an array (which you allocate with malloc), then uses assembler to nd the biggest of all the integers, then outputs it. You will need conditional-jump opcodes like jg. To use such an opcode, you usually first use an opcode like dec, or sub, which sets bits in the flags register. Then you will, for example use jg to jump somewhere (eg the top of the loop) if the result of the previous operation was greater than zero. You can also set the flags register using a cmp opcode. You will also need to access within an array, using base-displacement mode: mov eax,[ebx+ecx*4].
Extra credit: 5. maxf.c : same as above, but uses floats instead of integers.
This is what I have right now. When I input the list, it outputs the first number in the list, regardless of whether or not it is the biggest.
// 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;
}
cmp edx, ecx
compares integers, not floats. As bolov pointed out, there are different registers and instructions for floating point. – pcarter