More about Ian's Assembler/Machine Code Sinewave Demo Program
Version 3.0
Back to download page
View Program Source
This is for the windows program/application :-
Ian's Sinewave Machince Code Plotting Demo Program version 2.7
This
works ok on windows 8.1 and windows 10 computers.
What does this program do, and what is the significance of this program?
All it does is plot a sinewave on the screen when you type p on the keyboard.
The significance of the program is that the maths used to plot the sinewave is all done using
code written in machine code/assembler, and also not using the hardware floating point capability of
the computer, but using integer arithmetic.
This is why it is a machine code/assembler demo.
What is machine code/assembler?
Machine code is the raw bytes in an executable file (eg. a windows program/applications file) which are actually
run by the computer hardware. These are the codes in the file that tell the computer hardware (the CPU (Central Processing Unit))
what to do.
Assembly code is text in a program that generates these machine code codes very directly (that means that one bit of assembly code in the program generates just one bit of code in the file (called an op code, because it describes an operation for the CPU to do.)
There is a one to one correspondence between assembly code instructions and the machine code opcodes it generates/controls.
An example from this program is:-
mov edx, 20
which is an assembly language instruction to put the value 20 (decimal) in the edx register of the CPU.
Most of the code in this program is written in the high level language C, include the bit that actually draws the lines.:-
void zsimpline(long x1, long y1, long x2, long y2) {
MoveToEx(hDC, (int)(x1), (int)(y1), NULL);
LineTo(hDC, (int)x2, (int)y2);
}
This is an interface routine that calls the correct system calls (that's to the rest of the computer) that draw a line from
co-ordinates x1,y1 to x2,y2. That values of the co-ordinates (that's the numbers) are in the variables (computer storage locations)
called x1,y1, x2, and y2.
This C code text is compiled to produce machine code and this is a different and more complicated process than converting assembly language to machine code, which is called assembling, and is done by a simpler program called an assembler.
The machine code in this demo program calculates all the co-ordinates of all the lines to draw, and then tells the routine above (zsimpline()) where to draw the lines.
Another bit of high-level C code in the program is:-
if (wParam == 'p')asmtst_proc();
which means the the key you pressed is a 'p', then do the routine called
asmtst_proc(), which will draw the sinewave.
More about the program file..
This is a c file, compiled by Visual Studios Express 2015 (This is free from Microsoft and is compatible with windows 10 and windows 8.1,but not earlier versions of windows.)
I don't know how to make Visual Studios Express compile code for other(earlier) versions of windows.
The assembler code is embedded in this C program in the routine zsinewave().
If you want to compile it using Visual Studios Express just download all the files in the zip file (see program source page ) and just include them
in a new empty windows C++ windows project file. It should then compile fine. Note it is a .c file, not a .cpp file and contains C code not C++ code. Visual Studios seems to support this ok.
Can I compile it using LCC (free windows C compiler)
Not this version of the demo program because this version uses Intel Assembler code syntax, and LCC uses AT&T assembler code syntax,
so you would have to re-write it using the different assembler text codes. The same instructions (re-written in AT&T) would still work though
if compiled using LCC.
The C windows code parts of the program appear to be totally compatible though.
Machine code plotting routine zsinewave()
This is the bit of the program which is written in machine code and which
calculates the values of all the lines that draw the sinewave.
(To find it in the source code click here .)
The algorithm it uses is the iterative loop
y=y+y2*c;
y2=y2-y*c;
where c is a constant fraction that determines the frequence per unit iteration.As the computer recalculates these values (numbers) again and again as it iterates the values oscillate like an electronic LCR (coil and capacitor) circuit
and creates a sinewave.
The value in the computer register eax is y, and in the computer register ebx is y2,
and the value in computer register ecx is the x co-ordinate which corresponds to time in an electronic circuit.
eax, ebx and ecx are 32 bit registers in the computer core CPU on a windows PC.
What this means is that the rate of change (y2) of the height of the curve changes according to the inverse/negation of the height of the curve (y).
If you try thinking about that or doing it with each hand (one for the height of the curve and one for the rate of change) you should manage
to figure out what it does, which is both hands go up and down in a sinewave, 90o out of phase with each other.