I've made another program in my programming language targeting Linux and GNU Assembler, an analog clock. Here it goes:
Here is what it outputs right now (at 19:09):
It's a bit weird that, in order to effectively work in your own programming language, you need to insert tons of assembly code which you yourself don't fully understand.
Code:
Syntax GAS
;This is yet another example of how to target Linux using GNU Assembler.
AsmStart ;What follows is code produced by GCC 9.3.0 on Linux, I don't understand much of it either, and it's not important.
.file "analogClock.c"
.text
.comm result,4,4
.comm i,4,4
.comm x,4,4
.comm y,4,4
.comm currentSign,4,4
.comm centerX,4,4
.comm centerY,4,4
.comm distance,4,4
.comm clockRadius,4,4
.comm output,7360,32
.comm hour,4,4
.comm minute,4,4
.comm second,4,4
.comm angle,4,4
.comm endOfTheHandX,4,4
.comm endOfTheHandY,4,4
.comm coefficientOfTheDirection,4,4
.comm windowWidth,4,4
.comm windowHeight,4,4
.comm lowerBoundX,4,4
.comm upperBoundX,4,4
.comm lowerBoundY,4,4
.comm upperBoundY,4,4
.comm isXWithinBounds,4,4
.comm isYWithinBounds,4,4
.comm expectedY,4,4
.comm expectedX,4,4
.comm j,4,4
.comm ASCIIofSpaceAsFloat32,4,4
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
leal 4(%esp), %ecx
.cfi_def_cfa 1, 0
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
.cfi_escape 0x10,0x5,0x2,0x75,0
movl %esp, %ebp
pushl %ecx
.cfi_escape 0xf,0x3,0x75,0x7c,0x6
subl $36, %esp
subl $12, %esp
leal -20(%ebp), %eax
pushl %eax
call time
addl $16, %esp
subl $12, %esp
leal -20(%ebp), %eax
pushl %eax
call localtime
addl $16, %esp
movl %eax, -16(%ebp)
movl -16(%ebp), %eax
movl 8(%eax), %eax
movl %eax, -28(%ebp)
fildl -28(%ebp)
fstps hour
movl -16(%ebp), %eax
movl 4(%eax), %eax
movl %eax, -28(%ebp)
fildl -28(%ebp)
fstps minute
movl -16(%ebp), %eax
movl (%eax), %eax
movl %eax, -28(%ebp)
fildl -28(%ebp)
fstps second
#APP
AsmEnd ;And now finally follows a program written in AEC.
windowWidth:=80
windowHeight:=23
ASCIIofSpace<=" \0\0\0" ;As integer. We know we are dealing with a...
ASCIIofNewLine<="\n\0\0\0" ;32-bit low-endian machine.
ASCIIofStar<="*\0\0\0"
i:=0
While i<windowWidth*windowHeight ;First, fill the window with spaces and newlines.
If mod(i,windowWidth)=windowWidth-1
AsmStart
.intel_syntax noprefix
fild dword ptr ASCIIofNewLine
fstp dword ptr currentSign
.att_syntax
AsmEnd
Else
AsmStart
.intel_syntax noprefix
fild dword ptr ASCIIofSpace
fstp dword ptr currentSign
fld dword ptr currentSign
fstp dword ptr ASCIIofSpaceAsFloat32
.att_syntax
AsmEnd
EndIf
output[i]:=currentSign
i:=i+1
EndWhile
centerX:=windowWidth/2-mod(windowWidth/2,1)
centerY:=windowHeight/2-mod(windowHeight/2,1)
clockRadius:=(centerX<centerY)?(centerX):(centerY)-1
i:=0
While i<windowWidth*windowHeight ;Next, draw the circle which represents the clock.
y:=i/windowWidth-mod(i/windowWidth,1) ;When I didn't put "floor" into my programming language...
x:=mod(i,windowWidth)
distance:=sqrt((x-centerX)*(x-centerX)+(y-centerY)*(y-centerY)) ;Pythagorean Theorem.
If abs(distance-clockRadius)<3/4
AsmStart
.intel_syntax noprefix
fild dword ptr ASCIIofStar
fstp dword ptr currentSign
.att_syntax
AsmEnd
output[i]:=currentSign
EndIf
i:=i+1
EndWhile
AsmStart
.intel_syntax noprefix
jmp ASCIIofDigits$
ASCIIofDigits:
.macro writeDigits startingWith=0
.byte '0'+\startingWith,0,0,0 #".byte" is to GNU Assembler about the same as "db" is to FlatAssembler.
.if \startingWith < 9
writeDigits \startingWith+1
.endif
.endm
writeDigits #The goal is to make Assembler output the ASCII of "0\0\0\01\0\0\02\0\0\0...9\0\0\0" inside the executable (if the instruction pointer points to it, it will, of course, be an invalid instruction).
ASCIIofDigits$:
.att_syntax
AsmEnd
;Label of "12"...
AsmStart
.intel_syntax noprefix
fild dword ptr [ASCIIofDigits+1*4] #The ASCII of '1'.
fstp dword ptr currentSign
.att_syntax
AsmEnd
output[(centerY-clockRadius+1)*windowWidth+centerX]:=currentSign
AsmStart
.intel_syntax noprefix
fild dword ptr [ASCIIofDigits+2*4] #The ASCII of '2'.
fstp dword ptr currentSign
.att_syntax
AsmEnd
output[(centerY-clockRadius+1)*windowWidth+centerX+1]:=currentSign
AsmStart
.intel_syntax noprefix
fild dword ptr [ASCIIofDigits+6*4] #The ASCII of '6'.
fstp dword ptr currentSign
.att_syntax
AsmEnd
output[(centerY+clockRadius-1)*windowWidth+centerX]:=currentSign
AsmStart
.intel_syntax noprefix
fild dword ptr [ASCIIofDigits+3*4] #The ASCII of '3'.
fstp dword ptr currentSign
.att_syntax
AsmEnd
output[centerY*windowWidth+centerX+clockRadius-1]:=currentSign
AsmStart
.intel_syntax noprefix
fild dword ptr [ASCIIofDigits+9*4] #The ASCII of '9'.
fstp dword ptr currentSign
.att_syntax
AsmEnd
output[centerY*windowWidth+centerX-clockRadius+1]:=currentSign
AsmStart
.intel_syntax noprefix
fild dword ptr [ASCIIofDigits+1*4] #The ASCII of '1'.
fstp dword ptr currentSign
.att_syntax
AsmEnd
y:=centerY-(clockRadius-1)*cos(360/12)
y:=y-mod(y,1)
output[y*windowWidth+centerX+sin(360/12)*(clockRadius-1)]:=currentSign
AsmStart
.intel_syntax noprefix
fild dword ptr [ASCIIofDigits+2*4] #The ASCII of '2'.
fstp dword ptr currentSign
.att_syntax
AsmEnd
y:=centerY-(clockRadius-1.5)*cos(2*360/12)
y:=y-mod(y,1)
output[y*windowWidth+centerX+sin(2*360/12)*(clockRadius-1.5)]:=currentSign
AsmStart
.intel_syntax noprefix
fild dword ptr [ASCIIofDigits+4*4] #The ASCII of '4'.
fstp dword ptr currentSign
.att_syntax
AsmEnd
y:=centerY-(clockRadius-1)*cos(4*360/12)
y:=y-mod(y,1)
output[y*windowWidth+centerX+sin(4*360/12)*(clockRadius-1)]:=currentSign
AsmStart
.intel_syntax noprefix
fild dword ptr [ASCIIofDigits+5*4] #The ASCII of '5'.
fstp dword ptr currentSign
.att_syntax
AsmEnd
y:=centerY-(clockRadius-1)*cos(5*360/12)
y:=y-mod(y,1)
output[y*windowWidth+centerX+sin(5*360/12)*(clockRadius-1)]:=currentSign
AsmStart
.intel_syntax noprefix
fild dword ptr [ASCIIofDigits+7*4] #The ASCII of '7'.
fstp dword ptr currentSign
.att_syntax
AsmEnd
y:=centerY-(clockRadius-1)*cos(7*360/12)
y:=y-mod(y,1)
output[y*windowWidth+centerX+sin(7*360/12)*(clockRadius-1)]:=currentSign
AsmStart
.intel_syntax noprefix
fild dword ptr [ASCIIofDigits+8*4] #The ASCII of '8'.
fstp dword ptr currentSign
.att_syntax
AsmEnd
y:=centerY-(clockRadius-1)*cos(8*360/12)
y:=y-mod(y,1)
output[y*windowWidth+centerX+sin(8*360/12)*(clockRadius-1)]:=currentSign
;Label "10"...
AsmStart
.intel_syntax noprefix
fild dword ptr [ASCIIofDigits+1*4] #The ASCII of '1'.
fstp dword ptr currentSign
.att_syntax
AsmEnd
y:=centerY-(clockRadius-1.5)*cos(10*360/12)
y:=y-mod(y,1)
output[y*windowWidth+centerX+sin(10*360/12)*(clockRadius-1.5)]:=currentSign
AsmStart
.intel_syntax noprefix
fild dword ptr [ASCIIofDigits+0*4] #The ASCII of '0'.
fstp dword ptr currentSign
.att_syntax
AsmEnd
y:=centerY-(clockRadius-1.5)*cos(10*360/12)
y:=y-mod(y,1)
output[y*windowWidth+centerX+sin(10*360/12)*(clockRadius-1.5)+1]:=currentSign
;Label "11"...
AsmStart
.intel_syntax noprefix
fild dword ptr [ASCIIofDigits+1*4] #The ASCII of '1'.
fstp dword ptr currentSign
.att_syntax
AsmEnd
y:=centerY-(clockRadius-1.5)*cos(11*360/12)
y:=y-mod(y,1)
output[y*windowWidth+centerX+sin(11*360/12)*(clockRadius-1.5)]:=currentSign
AsmStart
.intel_syntax noprefix
fild dword ptr [ASCIIofDigits+1*4] #The ASCII of '1'.
fstp dword ptr currentSign
.att_syntax
AsmEnd
y:=centerY-(clockRadius-1.5)*cos(11*360/12)
y:=y-mod(y,1)
output[y*windowWidth+centerX+sin(11*360/12)*(clockRadius-1.5)+1] := currentSign
j:=0
While j<3
If j=0
angle:=(mod(hour+minute/60,12))*(360/12)
ElseIf j=1
angle:=minute*(360/60)
Else
angle:=second*(360/60)
EndIf
endOfTheHandX:=centerX+sin(angle)*clockRadius/(j=0?2:j=1?3/2:4/3) ;Hour hand will be the shortest, and the hand that shows the seconds will be the longest.
endOfTheHandY:=centerY-cos(angle)*clockRadius/(j=0?2:j=1?3/2:4/3)
coefficientOfTheDirection:=(endOfTheHandY-centerY)/(endOfTheHandX-centerX)
debugString <= "Drawing line between (%d,%d) and (%d,%d).\n\0"
AsmStart
.intel_syntax noprefix
.ifdef DEBUG #Conditional assembly, this will only be assembled if you tell GNU Assembler (by modifying the file or using command line) that you want to enable debugging.
fld dword ptr endOfTheHandY
fistp dword ptr result
push dword ptr result #This (pushing a "dword" onto the system stack) breaks the compatibility with 64-bit Linux (but you can still enable it by disabling debugging)!
fld dword ptr endOfTheHandX
fistp dword ptr result
push dword ptr result
fld dword ptr centerY
fistp dword ptr result
push dword ptr result
fld dword ptr centerX
fistp dword ptr result
push dword ptr result
lea ebx,debugString
push ebx
call printf
.endif #End of the conditional assembly.
.att_syntax
AsmEnd
i:=0
While i<windowWidth*windowHeight
lowerBoundX:=(endOfTheHandX<centerX)?(endOfTheHandX):(centerX)
upperBoundX:=(endOfTheHandX>centerX)?(endOfTheHandX):(centerX)
lowerBoundY:=(endOfTheHandY<centerY)?(endOfTheHandY):(centerY)
upperBoundY:=(endOfTheHandY>centerY)?(endOfTheHandY):(centerY)
y:=i/windowWidth-mod(i/windowWidth,1)
x:=mod(i,windowWidth)
isXWithinBounds:=(x>lowerBoundX | x=lowerBoundX) & (x<upperBoundX | x=upperBoundX) ;Damn... Now I understand why almost every programming language supports the "<=" and ">=" operators, no matter how much harder they make the language to tokenize.
isYWithinBounds:=(y>lowerBoundY | y=lowerBoundY) & (y<upperBoundY | y=upperBoundY)
If isXWithinBounds=1 & isYWithinBounds=1
expectedY:=(x-centerX)*coefficientOfTheDirection+centerY
expectedX:=(y-centerY)*(1/coefficientOfTheDirection)+centerX
debugString1 <= "The point (%d,%d) is within bounds, expectedY is %d and expectedX is %d.\n\0"
AsmStart
.intel_syntax noprefix
.ifdef DEBUG
fld dword ptr expectedX
fistp dword ptr result
push dword ptr result
fld dword ptr expectedY
fistp dword ptr result
push dword ptr result
fld dword ptr y
fistp dword ptr result
push dword ptr result
fld dword ptr x
fistp dword ptr result
push dword ptr result
lea ebx,debugString1
push ebx
call printf
.endif
.att_syntax
AsmEnd
ASCIIofLetterH<="h\0\0\0"
ASCIIofLetterM<="m\0\0\0"
ASCIIofLetterS<="s\0\0\0"
If j=0
AsmStart
.intel_syntax noprefix
fild dword ptr ASCIIofLetterH
fstp dword ptr currentSign
.att_syntax
AsmEnd
ElseIf j=1
AsmStart
.intel_syntax noprefix
fild dword ptr ASCIIofLetterM
fstp dword ptr currentSign
.att_syntax
AsmEnd
Else
AsmStart
.intel_syntax noprefix
fild dword ptr ASCIIofLetterS
fstp dword ptr currentSign
.att_syntax
AsmEnd
EndIf
If (upperBoundX=lowerBoundX | upperBoundY=lowerBoundY) & output[i]=ASCIIofSpaceAsFloat32
output[i]:=currentSign
EndIf
If (abs(expectedY-y)<3/4 | abs(expectedX-x)<3/4) & output[i]=ASCIIofSpaceAsFloat32
output[i]:=currentSign
EndIf
EndIf
i:=i+1
EndWhile
j:=j+1
EndWhile
;Draw some ornament...
ASCIIofLetterX<="x\0\0\0"
AsmStart
.intel_syntax noprefix
fild dword ptr ASCIIofLetterX
fstp dword ptr currentSign
.att_syntax
AsmEnd
i:=0
While i<windowWidth*windowHeight
y:=i/windowWidth-mod(i/windowWidth,1)
x:=mod(i,windowWidth)
If abs(windowHeight-2*ln(1+abs((x-centerX)/2))-y)<1-abs(x-centerX)/(centerX*95/112) & x>1/2*centerX & x<3/2*centerX & output[i]=ASCIIofSpaceAsFloat32 ;The logarithmic curve looks somewhat like a lemma of a flower.
output[i]:=currentSign
EndIf
i:=i+1
EndWhile
AsmStart ;And here goes how, according to GCC 9.3.0, you print the table and finish an Assembly program on 32-bit Linux (I don't understand that either, and it's not important).
#NO_APP
movl $0, -12(%ebp)
jmp .L2
.L3:
movl -12(%ebp), %eax
movss output(,%eax,4), %xmm0
cvttss2sil %xmm0, %eax
subl $12, %esp
pushl %eax
call putchar
addl $16, %esp
addl $1, -12(%ebp)
.L2:
cmpl $1839, -12(%ebp)
jle .L3
movl $0, %eax
movl -4(%ebp), %ecx
.cfi_def_cfa 1, 0
leave
.cfi_restore 5
leal -4(%ecx), %esp
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (GNU) 9.3.0"
.section .note.GNU-stack,"",@progbits
AsmEnd
Code:
*******
*** 12 ***
***11 1***
** **
* *
**10 2**
* m *
** mm **
* mm *
* m *
*9 h 3*
* hh *
* hh *
** sh **
*8 ssh 4*
** ss **
* *
xxx ** 7 5 ** xxx
xxxxxxxx *** *** xxxxxxxx
xxxxx*** 6 ***xxxxx
xxx*******xxx
xxx xxx