CMPS-2240 Lab-11
x86 inline assembly

Gordon Griesel
Department of Computer and Electrical Engineering and Computer Science
California State University, Bakersfield

Your personal computer: PuTTY and Xming.

Homework is here...
1. Copy and paste the following code into your circle.c program. Put it near the Bresenham Circle function. We will do this in class together on Friday. #define SWAP(x,y) (x)^=(y);(y)^=(x);(x)^=(y) void myBresenhamLine(int x0, int y0, int x1, int y1) { //Bresenham's line algorithm. integers only! int steep = (((y1-y0)/(x1-x0)) > 1 || ((y1-y0)/(x1-x0)) < -1); int steep = (abs(y1-y0) > abs(x1-x0)); if (steep) { SWAP(x0, y0); SWAP(x1, y1); } if (x0 > x1) { SWAP(x0, x1); SWAP(y0, y1); } int ystep = (y1>y0) ? 1 : -1; int yDiff = abs(y1-y0); int xDiff = x1 - x0; int err = xDiff >> 1; int x, y = y0; for (x=x0; x<=x1; x++) { if (steep) setPixel(y, x); else setPixel(x, y); err -= yDiff; if (err <= 0) { y += ystep; err += xDiff; } } } 2. Draw some objects using the Bresenham's line algorithm. We will do this together in class Friday. 3. Convert the SWAP macro to x86-64 inline assembly code. Swapping will now done with a function call. 4. Convert the following section of code to an x86 function: err -= yDiff; if (err <= 0) { y += ystep; err += xDiff; } 5. Convert the following sections of code (in bold) to x86 functions: int ystep = (y1>y0) ? 1 : -1; int yDiff = abs(y1-y0);
How to render a circle using lines.
Code written in Friday class. Put this at the end of the render() function. //draw a line myBresenhamLine(10,10,100,40); //now draw a circle int center[2] = { 300, 100 }; double radius = 195.0; double angle = 0.0; int n = 40; double inc = (2.0 * 3.14159265358979) / (double)n; double prevx, prevy; int i; for (i=0; i<=n; i++) { double x = cos(angle) * radius + center[0]; double y = sin(angle) * radius + center[1]; angle = angle + inc; setPixel(x, y); if (i > 0) { myBresenhamLine(prevx, prevy, x, y); } prevx = x; prevy = y; }
Program fix... Please copy the following code into your checkMouse() and checkKeys() functions. void checkMouse(XEvent *e) { static int savex = 0; static int savey = 0; if (e->type != ButtonPress && e->type != ButtonRelease && e->type != MotionNotify) { return; } int checkKeys(XEvent *e) { if (e->type != KeyPress && e->type != KeyRelease) { return 0; }
End of homework section

Introduction

This lab is about writing x86 inline assembly in a C program.
You will start with a working C program.
Then you will convert some code into x86 assembly code.

inline assembly samples <----from our lecture.


Goals:
Refactor C/C++ code using x86 inline assembly. Modify a small assembly program.

Files Needed: circle.c  showdot.s  Makefile

Path to the files: /home/fac/gordon/public_html/2240/code/lab11

Special instructions: Log into the Odin server like this... ssh -YC username@odin Y = pass X11 graphic information to the client C = attempt to compress the data

Getting started:

Do your work in your Odin 2240/b folder.

Copy the files into your 2240/b folder.

Build the program with: make

Run the program with: ./a.out

Assignment steps:

1. Draw a dot at the center of the circles by modifying the showdot.s program.
   
   The showDot function accepts 2 arguments indicating the x and y window
   coordinates of the dot (pixel) to be drawn.

2. Draw one or more circles inside the large yellow circle.
   Convert some or all of the Bresenham algorithm to inline assembly.

   Your new circles will be drawn in function inlineBresenhamCircle().

   Look at the notes in the programs for detailed instructions.

   We will get started together.

These files will be collected...

    /2240/b/circle.c
    /2240/b/showdot.s
    /2240/b/Makefile