Skip to content

Instantly share code, notes, and snippets.

@ejamesc
Created September 6, 2011 14:15
Show Gist options
  • Save ejamesc/1197655 to your computer and use it in GitHub Desktop.
Save ejamesc/1197655 to your computer and use it in GitHub Desktop.
CS2104 Vanilla Assembly Language Linked List
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* gobal declarations */
int eax, ebx, ecx, edx, esi, edi;
unsigned char M[10000];
int a[5];
/* prototypes */
void exec();
int main ()
{
/* declarations */
srand(time(NULL));
a[0] = 5;
a[1] = 2;
a[2] = 4;
a[3] = 21;
a[4] = 5;
/* Assignment of array to M[]
Srand is used to generate random address and may cause clashes, but
that is quite improbable. */
int curr = rand() % 10000;
*(int *)&M[curr] = a[0]; // initial value
int next = rand() % 10000; // randomly generate next address
*(int *)&M[curr+4] = next; // assignment of next add to initial node
esi = curr; // initial address
curr = next;
int i;
for (i=1; i<=5; i++)
{
*(int *)&M[curr] = a[i];
next =(i == 5) ? 0 : rand() % 10000;
*(int *)&M[curr+4] = next;
curr = next;
}
// Execute VAL code
exec();
printf("result = %d\n", eax);
return 0;
}
void exec()
{
eax = 0;
loop:
ebx = *(int *)&M[esi]; // assign value of node to ebx
eax += ebx;
esi = *(int *)&M[esi+4]; // address of next node
if (esi < 1) goto endloop; // end condition: esi < 1
goto loop;
endloop:{}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment