Hi all,
how could you initialize, through a loop, a c array?
For instance, say I have

#define SIZE 10
int test[SIZE];
for (unsigned int i = 0; i < SIZE; i++) {
test[i] = 0;
}

How can I make use of some Assembly to loop through test?
I know that's absolutely not a good way to initialize an array, but I'd like to see how that can be done in asm.
PS: AT&T syntax :)
Thanks

Do mean inline asm?

Yes sir

Well let me start off by saying memset is the proper way to initialize an array but if you want to see AT & T inline asm then you really should check the many docs on the internet..Here's my very simple version..Note its for a 64 bit Intel/AMD system.

include <stdio.h>

#define SIZE 10
int test[SIZE];

int main()
{
	int i = 0;

	for (i = 0; i < SIZE; ++i)
		test[i] = i + 3;

	for (i = 0; i < SIZE; ++i)
		fprintf(stdout, "ans->%d\n", test[i]);

	__asm__
	(
		"pushq	%rdi\n\t"
		"pushq	%rax\n\t"

		"xorq	%rdi, %rdi\n\t"		

		"call	1f\n\t"
		"1:\n\t"
		"popq	%rax\n\t"
		"addq	$5, %rax\n\t"
		
		"cmpq	$9, %rdi\n\t"
		"je	2f\n\t"

		"movq	$0, test(, %rdi, 4)\n\t"

		"incq	%rdi\n\t"
		"jmp	*%rax\n\t"

		"2:\n\t"
		"popq	%rax\n\t"
		"popq	%rdi\n\t"
	);

	for (i = 0; i < SIZE; ++i)
		fprintf(stdout, "ans->%d\n", test[i]);
	return 0;
}

Output on my system

ans->3
ans->4
ans->5
ans->6
ans->7
ans->8
ans->9
ans->10
ans->11
ans->12
ans->0
ans->0
ans->0
ans->0
ans->0
ans->0
ans->0
ans->0
ans->0
ans->0

Here's a cleaner version that uses the proper way to jump back.

#include <stdio.h>

#define SIZE 10
int test[SIZE];

int main()
{
	int i = 0;

	for (i = 0; i < SIZE; ++i)
		test[i] = i + 3;

	for (i = 0; i < SIZE; ++i)
		fprintf(stdout, "ans->%d\n", test[i]);

	__asm__
	(
		"pushq	%rdi\n\t"

		"xorq	%rdi, %rdi\n\t"		

		"1:\n\t"
		
		"cmpq	$9, %rdi\n\t"
		"je	2f\n\t"

		"movq	$0, test(, %rdi, 4)\n\t"

		"incq	%rdi\n\t"
		"jmp	1b\n\t"

		"2:\n\t"

		"popq	%rdi\n\t"
	);

	for (i = 0; i < SIZE; ++i)
		fprintf(stdout, "ans->%d\n", test[i]);
	return 0;
}

Thank you very much. That's what I was looking for. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.