Skip to content

Commit

Permalink
Adding inline exercises to lab/interactiune-c-assembly
Browse files Browse the repository at this point in the history
Adding 1-inline-for, 2-inline-rotate and 3-inline-rtdscp
and reordering the exercises accordingly.

Signed-off-by: Andreia Ocanoaia <[email protected]>
  • Loading branch information
andreia-oca committed May 3, 2024
1 parent 8068505 commit 75a7501
Show file tree
Hide file tree
Showing 121 changed files with 250 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/inline_for.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PROGNAME := inline_for
include ../../utils/Makefile.generic

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: BSD-3-Clause

#include <stdio.h>

#define NUM 100

int main(void)
{
size_t n = NUM;
size_t sum = 0;

__asm__ (
"xor eax, eax\n\t" /* collect the sum in eax */
/* use ecx to go through items, start from n */
"mov ecx, %1\n"
"add_to_sum:\n\t"
"add eax, ecx\n\t"
"loopnz add_to_sum\n\t"
/* place sum in output register */
"mov %0, eax\n\t"
: "=r" (sum) /* output variable */
: "r" (n) /* input variable */
: "eax", "ecx"
); /* registers used in the assembly code */

printf("sum(%u): %u\n", n, sum);

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/inline_rotate.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PROGNAME := inline_rotate
include ../../utils/Makefile.generic

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: BSD-3-Clause

#include <stdio.h>

#define NUM 0x12345678

int main(void)
{
size_t n = NUM;
size_t rot_left = 0;
size_t rot_right = 0;

__asm__ (""
/* TODO: Use rol instruction to shift n by 8 bits left.
* Place result in rot_left variable.
*/

/* TODO: Use ror instruction to shift n by 8 bits right.
* Place result in rot_right variable.
*/
/* TODO: Declare output variables - preceded by ':'. */
/* TODO: Declare input variables - preceded by ':'. */
/* TODO: Declared used registers - preceded by ':'. */
);

/* NOTE: Output variables are passed by address, input variables
* are passed by value.
*/

printf("init: 0x%08x, rot_left: 0x%08x, rot_right: 0x%08x\n",
n, rot_left, rot_right);

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/inline_cpuid.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PROGNAME := inline_rtdscp
include ../../utils/Makefile.generic

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: BSD-3-Clause

#include <stdio.h>

int main(void)
{
char rdtscp_str[13];

__asm__ (""
/* TODO: Make rdtscp call and copy string in rdtscp_str.
* After rdtscp call result is placed in (EDX:EAX registers).
*/
);

rdtscp_str[12] = '\0';

printf("rdtscp string: %s\n", rdtscp_str);

return 0;
}
147 changes: 106 additions & 41 deletions laborator/content/interactiune-c-assembly/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/inline_rotate.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PROGNAME := inline_rotate
include ../../../utils/Makefile.generic

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: BSD-3-Clause

#include <stdio.h>

#define NUM 0x12345678

int main(void)
{
size_t n = NUM;
size_t rot_left = 0;
size_t rot_right = 0;

__asm__ (
/* Use rol instruction to shift n by 8 bits left.
* Place result in rot_left variable.
*/
"mov eax, %1\n\t"
"rol %1, 8\n\t"
"mov %0, eax\n\t"
: "=r" (rot_left)
: "r" (n)
: "eax"
);

__asm__ (
/* Use ror instruction to shift n by 8 bits right.
* Place result in rot_right variable.
*/
"mov eax, %1\n\t"
"ror eax, 8\n\t"
"mov %0, eax\n\t"
: "=r" (rot_right)
: "r" (n)
: "eax", "ecx"
);


/* NOTE: Output variables are passed by address, input variables
* are passed by value.
*/
printf("init: 0x%08x, rot_left: 0x%08x, rot_right: 0x%08x\n",
n, rot_left, rot_right);

return 0;
}

0 comments on commit 75a7501

Please sign in to comment.