Prev: Win32 non blocking console input?
Next: hugi compo #29
From: NathanCBaker on 30 Nov 2008 15:57 This is crude (un-polished) mock-up of a Fifteen (Sliding) Puzzle game. #include( "stdlib.hhf" ) type blank:class var column :int32; row :int32; left :boolean; right :boolean; up :boolean; down :boolean; board :dword; procedure create( field:dword ); procedure check( x:int32; y:int32 ); procedure mleft; procedure mright; procedure mup; procedure mdown; procedure show; endclass; static vmt(blank); var player :blank; procedure blank.create( field:dword ); begin create; push( eax ); push( ebx ); push( ecx ); push( edx ); if( esi = 0 ) then mem.alloc( @size( blank )); mov( eax, esi ); endif; mov( &blank._VMT_, this._pVMT_ ); mov( field, ebx ); mov( ebx, this.board ); for( mov( 0, ecx ); ecx < 16; inc( ecx ) ) do mov( 0, (type byte [ebx+ecx]) ); endfor; for( mov( 1, ecx ); ecx < 16; inc( ecx ) ) do xor( eax, eax ); while (eax = 0) do rand.urange( 0, 15 ); mov( (type byte [ebx+eax]), dl ); if ( dl = 0 ) then mov( cl, (type byte [ebx+eax]) ); if ( eax = 0 ) then mov( 1, eax ); endif; else xor( eax, eax ); endif; endwhile; endfor; for( mov( 0, ecx ); ecx < 16; inc( ecx ) ) do mov( (type byte [ebx+ecx]), al ); breakif( al = 0 ); endfor; mov( ecx, eax ); stdout.put( "zero: ", al, nl ); mov( 4, ecx ); cdq(); div( ecx ); mov( eax, this.column ); mov( edx, this.row ); this.check( eax, edx ); pop( edx ); pop( ecx ); pop( ebx ); pop( eax ); end create; procedure blank.check( x:int32; y:int32 ); begin check; push( eax ); push( edx ); mov( x, eax ); mov( y, edx ); if( eax = 0 ) then mov( false, this.left ); else mov( true, this.left ); endif; if( eax = 3 ) then mov( false, this.right ); else mov( true, this.right ); endif; if( edx = 0 ) then mov( false, this.up ); else mov( true, this.up ); endif; if( edx = 3 ) then mov( false, this.down ); else mov( true, this.down ); endif; pop( edx ); pop( eax ); end check; procedure blank.mleft; begin mleft; push( eax ); push( ebx ); push( ecx ); push( edx ); mov( 4, eax ); mul( this.column ); add( this.row, eax ); //stdout.put( eax, nl ); mov( eax, ecx ); mov( 4, eax ); dec( this.column ); mul( this.column ); add( this.row, eax ); //stdout.put( eax, nl ); mov( this.board, ebx ); mov( (type byte [ebx+eax]), dl ); mov( dl, (type byte [ebx+ecx]) ); xor( edx, edx ); mov( dl, (type byte [ebx+eax]) ); this.check( this.column, this.row ); pop( edx ); pop( ecx ); pop( ebx ); pop( eax ); end mleft; procedure blank.mright; begin mright; push( eax ); push( ebx ); push( ecx ); push( edx ); mov( 4, eax ); mul( this.column ); add( this.row, eax ); //stdout.put( eax, nl ); mov( eax, ecx ); mov( 4, eax ); inc( this.column ); mul( this.column ); add( this.row, eax ); //stdout.put( eax, nl ); mov( this.board, ebx ); mov( (type byte [ebx+eax]), dl ); mov( dl, (type byte [ebx+ecx]) ); xor( edx, edx ); mov( dl, (type byte [ebx+eax]) ); this.check( this.column, this.row ); pop( edx ); pop( ecx ); pop( ebx ); pop( eax ); end mright; procedure blank.mup; begin mup; push( eax ); push( ebx ); push( ecx ); push( edx ); mov( 4, eax ); mul( this.column ); add( this.row, eax ); //stdout.put( eax, nl ); mov( eax, ecx ); mov( 4, eax ); mul( this.column ); dec( this.row ); add( this.row, eax ); //stdout.put( eax, nl ); mov( this.board, ebx ); mov( (type byte [ebx+eax]), dl ); mov( dl, (type byte [ebx+ecx]) ); xor( edx, edx ); mov( dl, (type byte [ebx+eax]) ); this.check( this.column, this.row ); pop( edx ); pop( ecx ); pop( ebx ); pop( eax ); end mup; procedure blank.mdown; begin mdown; push( eax ); push( ebx ); push( ecx ); push( edx ); mov( 4, eax ); mul( this.column ); add( this.row, eax ); //stdout.put( eax, nl ); mov( eax, ecx ); mov( 4, eax ); mul( this.column ); inc( this.row ); add( this.row, eax ); //stdout.put( eax, nl ); mov( this.board, ebx ); mov( (type byte [ebx+eax]), dl ); mov( dl, (type byte [ebx+ecx]) ); xor( edx, edx ); mov( dl, (type byte [ebx+eax]) ); this.check( this.column, this.row ); pop( edx ); pop( ecx ); pop( ebx ); pop( eax ); end mdown; procedure blank.show; begin show; push( ebx ); push( ecx ); push( edx ); console.cls(); mov( this.board, ebx ); //for( mov( 0, ecx ); ecx < 16; inc( ecx ) ) do //mov( (type byte [ebx+ecx]), al ); //stdout.put( al, nl ); //endfor; for( mov( 0, edx); edx < 4; inc( edx ) ) do for( mov( 0, ecx ); ecx < 4 ; inc( ecx ) ) do mov( ecx, eax ); push( ecx ); mov( 4, ebx ); mul( bl ); add( edx, eax ); mov( this.board, ebx ); mov( (type byte [ebx+eax]), cl ); stdout.put( "| ", cl ); pop( ecx ); endfor; stdout.newln(); endfor; pop( edx ); pop( ecx ); pop( ebx ); end show; begin fifteen; rand.randomize(); mem.alloc( 16 ); player.create( eax ); //stdout.put( player.column, nl ); //stdout.put( player.row, nl ); player.show(); mov( true, edx ); while( edx = true ) do stdin.getc(); if ( al = 'q' ) then mov( false, edx ); elseif ( al = 'a' ) then if ( player.left = true ) then player.mleft(); player.show(); else stdout.putc( #07 ); stdout.putc( #07 ); player.show(); endif; elseif ( al = 'd' ) then if ( player.right = true ) then player.mright(); player.show(); else stdout.putc( #07 ); stdout.putc( #07 ); player.show(); endif; elseif ( al = 'w' ) then if ( player.up = true ) then player.mup(); player.show(); else stdout.putc( #07 ); stdout.putc( #07 ); player.show(); endif; elseif ( al = 's' ) then if ( player.down = true ) then player.mdown(); player.show(); else stdout.putc( #07 ); stdout.putc( #07 ); player.show(); endif; endif; endwhile; end fifteen; Nathan.
From: Herbert Kleebauer on 1 Dec 2008 07:33 NathanCBaker(a)gmail.com wrote: Can you post (a link to) the binary, so I can disassemble it to get some readable code? > mov( 4, ecx ); > cdq(); > div( ecx ); Does HLA now support optimization or do you really use the div instruction to dived by 4? > rand.randomize(); > mem.alloc( 16 ); > player.create( eax ); > player.show(); > mov( true, edx ); > while( edx = true ) do For which processor is the code. From the above instructions I only found the "mov" instruction in the x86 processor manual. Can you post (a link to) the binary, so I can disassemble it to get some readable code?
From: Frank Kotler on 2 Dec 2008 03:20 Rod Pemberton wrote: .... > I'll just assume > it's some twisted variation of NASM syntax. Ahem! I don't know who's more offended by that, Nasm or Randy! :) .... > Frank's been quiet... My momma taught me, "If you can't say something nice, don't say anything at all." (What'd yours teach you?) Couldn't get it to build, actually. My install of HLA is "crossed up" right now. Parts from the "new" library, and parts from "canonical" HLA (I *think* that's what's wrong with it...) Error in file "/usr/hla/include/os.hhf" at line 5 [errid:110999/hlaparse.c]: syntax error, unexpected namespaceTkn, expecting DoOneValStmt. Near: << namespace >> Or maybe it's one of those things you mention... > Coal for Randall... Randy's in California. Coal's illegal! Get him a lump of something with a nice blue glow. :) Thanks for the game, Nathan! I really *am* planning to reinstall HLA and take a look at it. Got a new version of Nasm to push out to SourceForge... maybe after my nap... Some one of you bright guys tell us what the problem is here: <https://sourceforge.net/forum/forum.php?thread_id=2625923&forum_id=167168> Best, Frank
From: Herbert Kleebauer on 2 Dec 2008 05:41 NathanCBaker(a)gmail.com wrote: > On Dec 1, 7:33 am, Herbert Kleebauer <k...(a)unibwm.de> wrote: > > Does HLA now support optimization or do you really use the div instruction to > > dived by 4? > > > > Optimization!?!? You want to optimize a simple console game?? I > think an explicit division instruction enhances the readability -- If you want readability, use a HLL (and then you also get speed because the compiler doesn't use the div instruction when you write /4 in your source). > which is probably more important than execution speed these days. > Heck, even 'xor eax, eax' is a bit steep for today's readers. Take a Then better don't do any assembly programming than this way. > You'll need the Randy Hyde x86 processor manual which describes these > fancy new machine instructions ( e.g. rand.randomize, mem.alloc, > etc. ): HLA is maybe the most important contribution in killing assembly programming (if it really is use for teaching assembly programming as Randy claims). Now compare your source code with this C program, which is better readable? #include <stdio.h> #define n (('|'<<24)+(' '<<16)+('0'<<8)) unsigned int feld[4][4]; int main() {int i,j,k,x=0,y=0; for (i=0; i<4; i++) for (j=0; j<4; j++) feld[i][j]= n + (((k='0'+i*4+j)<='9')?k:k-'0'-10+'a'); print(); while ((i=getchar())!='q') {switch(i) {case 'a': if (x==0) putchar(0x07); else {feld[x][y]=feld[x-1][y]; x--;} break; case 'd': if (x==3) putchar(0x07); else {feld[x][y]=feld[x+1][y]; x++;} break; case 'w': if (y==0) putchar(0x07); else {feld[x][y]=feld[x][y-1]; y--;} break; case 's': if (y==3) putchar(0x07); else {feld[x][y]=feld[x][y+1]; y++;} break; case '\n': continue; default: putchar(0x07); } feld[x][y]= n + '0'; print(); } } int print() {int i,j,k; for (i=0; i<4; i++) {for (j=0; j<4; j++) for (k=24; k>=0; k=k-8) putchar(feld[j][i]>>k); putchar('\n');} }
From: Jim Carlock on 2 Dec 2008 12:42
<NathanCBaker(a)gmail.com> wrote... <snip>...</snip> The message to Nathan appears after the message to Randall. This message goes to Randall... I tried to get hla.exe to output the parameters to a file as follows: Z:\source\hla\nathan>hla.exe -? >> hla.txt (1) I ended up with the same "Press the enter key to continue:" messages. It would be nice to get all those parameters into a text file in a normal convenient way. :-) I'll leave them posted here to help others out. (2) I ended up with ALOT of whitespace as well. It would be nice to NOT have ALL that WHITESPACE. I removed it from what follows in order to make it NICE for everyone here that wants to copy it. (3) I counted over 100 lines of whitespace between block of 12 or 13 lines. Usage: hla options filename(s) HLA (High Level Assembler - FASM back end, POLINK linker) Version 1.103 build 20419 (prototype) Generic Options: -license Display license information. -@ Do not generate linker response file. -@@ Force generation of a new linker response file. -dxx Define VAL symbol xx to have type BOOLEAN and value TRUE. -dxx=str Defile VAL symbol xx to have type STRING and value str. -sym Dump symbol table after compile. -test Send diagnostic info to stdout rather than stderr. -v Verbose compile (also sends output to stdout). -? Display this help message. Language Control: -level=h High-level assembly language. -level=m Medium-level assembly language. -level=l Low-level assembly language. -level=v Machine-level assembly language (very low level). Source Output Control: -sourcemode Compile to source instructions (rather than hex opcodes) -s Compile to .ASM files only (using default ASM syntax). -sh Compile to pseudo-HLA source file. (implies sourcemode). -sm Compile to MASM source files only. -sf Compile to FASM source files only. -sn Compile to NASM source files only. -st Compile to TASM source files only. -sg Compile to GAS source files only. -sx Compile to GAS source files for Mac OSX only. -code1st Emit machine instructions before data in code segment. -main:xxxx Use 'xxxx' as the name of the HLA main program. HLAPARSE Compiler/Back-end Assembler Output Control: -c Compile and assemble to object file only. -cf Compile and assemble to object file only (using FASM). -cn Compile and assemble to object file only (using NASM). -cm Compile/assemble to object using MASM (Windows only). -ct Compile/assemble to object using TASM (Windows only). -cg Compile/assemble to object using GAS (Linux/FreeBSD only). -cx Compile/assemble to object using GAS (Mac only). -co Compile/assemble to object using internal FASM back-end (Win32). -o:omf Produce OMF files (for Windows). -o:coff Produce win32 COFF files (for Windows). -o:elf Produce ELF files (for Linux). -axxxxx Pass xxxxx as command line parameter to assembler. Executable Output Control: -xf Compile/assemble/link to executable (using FASM). -xn Compile/assemble/link to executable (using NASM). -xm Compile/assemble/link to object using MASM (Windows only). -xt Compile/assemble/link to object using TASM (Windows only). -xg Compile/assemble/link to object using GAS (Linux/FreeBSD only). -xx Compile/assemble/link to object using GAS (Mac only). -xo Compile/assemble/link to object internal FASM back-end (Windows only). -win32 Generate code for Win32 OS. -linux Generate code for Linux OS. -freebsd Generate code for FreeBSD OS. -macos Generate code for Mac OSX. Linker Control: -lxxxxx Pass xxxxx as command line parameter to linker. -e:name Executable output filename (appends ".exe" under Windows). -x:name Executable output filename (does not append ".exe"). -m Create a map file during link -w Compile as windows app (default is console app). -polink Force use of Pelles C linker/resource compiler. -mslink Force use of Microsoft linker/resource compiler. Temporary file control and assembly control: -p:path Use <path> as the working directory for temporary files. (overrides hlatmp environment variable.) -r:name <name> is a text file containing cmd line options. -obj:path Use <path> as the directory to hold the object files. -i:path Include path (used to override HLAINC environment variable). -lib:path Library path (used to overide HLALIB environment variable). HLA Environment Variables: hlalib=<path> Sets path to hlalib.lib file (e.g., c:\hla\hlalib\hlalib.lib) hlainc=<path> Sets path to HLA include subdirectory (e.g., c:\hla\include) hlatmp=<path> Sets path to directory to hold temp files (optional) hlaasmopt=<options> Passes the specified command-line options on to the underlying assembler. hlalinkopt=<options> Passes the specified command-line options on to the underlying linker. More HLA Environment Variables: hla=<asm> Sets default assembler behavior <asm>: hla- uses internal version of FASM ohla- uses internal version of FASM fhla- uses FASM as the back-end assembler nhla- uses NASM as the back-end assembler Windows Only: mhla- uses MASM as the back-end assembler thla- uses TASM as the back-end assembler Linux Only: ghla- uses GAS as the back-end assembler hlalink=<lnkr> Sets default linker behavior <lnkr>: Windows Only: mslink- use Microsoft's link.exe linker polink- use the Pelles C polink.exe linker Linux Only: ld- use the FSF/GNU ld linker Z:\source\hla\nathan> And the message for Nathan, I compiled via the following: I downloaded the latest version of HLA 1.103 from the following link: HLA Upgrade for Windows (HLA v1.103, released 8/11/2008) http://webster.cs.ucr.edu/AsmTools/HLA/dnld.html I set HLAINC to the proper folder, and set HLALIB to the proper folder, which contain the new include and library files. I like some of the command-line options for HLA. I only need to figure out how to use hla.exe, now. Some help here would be nice. I tried the following: hla.exe -v sliders.hla And various combinations of the following... hla.exe -v -@@ -level=h -sm -c -o:coff -win32 -e:sliders -m -polink sliders.hla And one last question... which version of HLA are you compiling with? I believe I have the same problem Frank has, as I installed the latest version of HLA (HLA Update) on top of what was previously there and I end up with the following messages (errors?): Z:\source\hla\nathan>hla.exe -v sliders.hla HLA (High Level Assembler) Use '-license' to see licensing information. Version Version 1.103 build 20419 (prototype) Win32 COFF output OBJ output using internal FASM back-end -test active HLA Lib Path: Z:\hla\hlalib HLA include path: Z:\hla\include HLA temp path: Linker Lib Path: Z:\PSDK\20010627\Lib\.;Z:\hla Files: 1: sliders.hla Compiling 'sliders.hla' to 'sliders.obj' using command line: [hlaparse -WIN32 -level=high -v -sf -ccoff -test "sliders.hla"] ---------------------- HLA (High Level Assembler) Parser use '-license' to view license information Version Version 1.103 build 20418 (prototype) -t active File: sliders.hla Output Path: "" hlainc Path: "Z:\hla\include" Compiler running under Windows OS Back-end assembler: FASM Language Level: high Compiling "sliders.hla" to "sliders.obj" Error in file "os.hhf" at line 5 syntax error, unexpected namespaceTkn, expecting DoOneValStmt. Near: << namespace >> Compilation complete, 5 lines, 0.110 seconds, 45 lines/second Using flat assembler version C1.66 os.hhf [4]: error: illegal instruction. Z:\source\hla\nathan> The puzzling thing above, is that "Linker Lib Path:" line. Linker Lib Path: Z:\PSDK\20010627\Lib\.;Z:\hla Where and why does it say "Z:\hla" ? -- Jim Carlock |