flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > Examples and Tutorials > New Z Language+Library + 10 Examples

Author
Thread Post new topic Reply to topic
uart777



Joined: 17 Jan 2012
Posts: 369
New Z Language+Library + 10 Examples
Z - The Final Language+Library+Examples

Much clearer than previous code. It's been a while since I've updated the Z77 library and it has been improved greatly.

Includes edited FASM. Only one change: Optional ?if/?else/?while/etc for assembly-time directives. Defined if/else/while/etc macros for runtime. In functions, removed [] for locals and parameters.


Description:
Filesize: 48.29 KB
Viewed: 620 Time(s)

z.jpg


Description:
Download
Filename: !z.zip
Filesize: 390.15 KB
Downloaded: 81 Time(s)

Post 24 Oct 2013, 14:22
View user's profile Send private message Reply with quote
uart777



Joined: 17 Jan 2012
Posts: 369
Preview/Excerpts:

Code:
; n+(((p-1)-(n+p-1))&(p-1))

function align.nnp
 . r1=pr1-1r2=nr2+r1r0=r1,\
 r0-r2r0&r1r1=r0r2=nr0+r2
endf  

macro set.nibble vin { ; v|=(n<<(i*4))
 . r0=vr1=ir2=nr1<<2r2<<cl,\
 r0|r2v=r0
}     

function tripletabc
 . r0=ar0<<6r1=br1<<3r0|r1r0|c
endf 

function rgbrgb
 . r0=rr0<<16r1=gr1<<8,\
 r0|r1r2=br0|r2
endf  

; standard copy with 0 after

function text.copyab
 . r0=ar2=b
 while byte [r2]
  . cl=[r2], [r0]=cl,\
  r0++, r2++
 endw
 . byte [r0]=0
endf  

; attach b to a

function text.attachab
 text.n a
 . r0+a
 text.copy r0b
endf

; search 32BIT integer array for v.
; return address or 0

function search.navn
 . r0=ar1=v
 repeat n
  if [r0]=r1
   return
  end
  . r0+4
 endr
endf 0

; shuffle all array indices in a unique
; fashion with no duplicates

function array.randomizea
 locals ijnsmaxp
 . r0=as=[?array.s+r0],\
  n=[?array.n+r0], r1=n,\
  r1--, max=r1
 try p=allocate s
 loop i=0 to n
  get j=random max
  array.exchange aijp
 endl
 destroy p
endf

; 24/32 must be copied backwards because
; of overlap...

function convert.pixels.24.32pn
 locals i
 . r0=pr2=r0r1=n,\
  r0=&[r0+r1*4-4], r1*3r1-3r2+r1
 loop i=0 to n
  . r1=[r2], r1&0FFFFFFhr1<<8,\
  [r0]=r1r2-3r0-4
 endl
endf  

; rotate 'r'ight (90) or 'l'eft (270)

function rotate.imageimageway
 locals xywhpscolrow
 . r0=images=[?image.p+r0],\
  w=[?image.w+r0], h=[?image.h+r0]
 try p=scratch hw ; w/h reversed
 if way='r'         ; right, 90
  . r0=h,\
   r0--, col=r0
  loop y=0 to h
   loop x=0 to w
    . r0=xr0*hr0+colr0<<2,\
     r0+pr2=yr2*wr2+xr2<<2,\
    r2+s, [r0]=[r2]
   endl
   . col--
  endl
 else.if way='l' ; left, 270
  . col=0
  loop y=0 to h
   . r0=wr0--, row=r0
   loop x=0 to w
    . r0=rowr0*hr0+colr0<<2,\
     r0+pr2=yr2*wr2+xr2<<2,\
    r2+s, [r0]=[r2], row--
   endl
   . col++
  endl
 end
 end.scratch image
 . r0=image,\ ; adjust location
  r2=[?image.x+r0], r1=wr1>>1r2+r1,\
  r1=hr1>>1r2-r1, [?image.x+r0]=r2,\
  r2=[?image.y+r0], r1=hr1>>1r2+r1,\
  r1=wr1>>1r2-r1, [?image.y+r0]=r2
endf     

; size by percentage (integer)

function size.image.pimagep
 locals whiwih
 . r0=image,\
  iw=[?image.w+r0], ih=[?image.h+r0],\
  r0=iwr0*pr1=100r0/r1w=r0,\
  r0=ihr0*pr1=100r0/r1h=r0
 size.image imagewh
endf   

function mixabn
 locals srsgsbdrdgdb
 . r0=ar2=b

 . \ ; extract r/g/b components
  r1=r0r1>>16r1&0FFhsr=r1,\
  r1=r0r1>>8r1&0FFhsg=r1,\
  r1=r0r1&0FFhsb=r1,\
  r1=r2r1>>16r1&0FFhdr=r1,\
  r1=r2r1>>8r1&0FFhdg=r1,\
  r1=r2r1&0FFhdb=r1

 . \ ; calculate deltas: (((s-d)*a)/256)+d
  r0=srr0-drr0*nr0>>8r0+dr,\
  r1=sgr1-dgr1*nr1>>8r1+dg,\
  r2=sbr2-dbr2*nr2>>8r2+db

 . \ ; construct RGB
  r0<<16r1<<8r0|r1r0|r2
endf 

; draw 32BPP scanline with alpha (AA.RR.GG.BBh).

; * 0     - 100% visible/opaque
; * 1-254 - transparent
; * 255   - 100% invisible (a&0FF000000h=0)

function draw.scanline.apixelsxyw
 locals i
 push r6 r7
 . r0=&ir6=&xr7=&yr1=&w
 clip.scanline r0r6r7r1
 fail .e
 vga.xy [r6], [r7]
 . r7=r0r6=pixelsr6+i

 repeat w           ; draw pixels
  . r0=[r6]         ; get pixel
  if r0&0FF000000h  ; alpha?
   . r1=r0r1>>>24
   if r1=0FFh       ; invisible
    jmp .next
   end
   mix [r7], r0r1 ; result in r0
  end
  . [r7]=r0         ; *vga++=*p++
  .next:
  . r6+4r7+4
 endr
 .e:
 pop r7 r6
endf

function load.filefile
 fault .0
 try open file
 try [file.p]=allocate [file.n]
 try read [file.p], [file.n]
 close
 . r1=[file.n]
 return [file.p]
 .0flush
 close
endf 0

function save.filefilepn
 fault .0
 try create file
 try write pn
 close
 return 1
 .0close
endf 0

function load.textfile
 fault .0
 try open file
 . r0=[file.n], r0++
 try [file.p]=allocate r0
 try read [file.p], [file.n]
 close
 . r0=[file.p],\
  r1=[file.n], byte [r0+r1]=0
 escape
 .0flush
 close
endf 0

function save.textfilet
 text.n t
 save.file filetr0
endf

function append.filefilepn
 fault .0
 !if openfile
  seek.r 0SEEK_END
 else
  try create file
 end
 try write pn
 close
 return file
 .0close
endf 0

Post 24 Oct 2013, 14:24
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 14483
Location: Can't remember
A lot of the syntax you use reminds me of C and the examples one sees with C code. Short generic function names. Short generic variable names. Dense coding that requires horizontal scanning. Little or no error checking. Lots of dots and other types of non-alphanumeric characters.
Post 24 Oct 2013, 14:39
View user's profile Send private message Visit poster's website Reply with quote
hopcode



Joined: 04 Mar 2008
Posts: 563
Location: Germany
Hallo everybody,

revolution wrote:
A lot of the syntax you use reminds me of C and the examples one sees with C code. Short generic function names. Short generic variable names. Dense coding that requires horizontal scanning. Little or no error checking. Lots of dots and other types of non-alphanumeric characters.

i partially agree with revolution,

pros
1) that language and functions do not break fasm assembler semantics. mov eax,eax is still allowed
2) most HLL coders do not require the bit/byte depth
3) the output in the picture is a real charming key-factor.

contras
1) horizontal scanning, yes. because of the inner language's partially-organized semantics
2) extensibility: a language that does not make OOP easier for example, i imagine for what i read there.

i like it anyway, because it is a starting point to think about a "raw language".
a language that is neither HLL nor assembly, once granted assembly remains allowed.
this will make portability easier, after considering graph primitives relying on a well-known
documented and used external libraries, as SDL or SFML may be on different development systems.

also, my tipps
1) reconsider semantics in a BNF way (http://de.wikipedia.org/wiki/Backus-Naur-Form) , as it seems i told you already in other thread
2) reconsider it for one more reason: extensibility (example OOP)

the time is mature to run and develop stuff on ARM or low power systems.
the dependency from a simple toolchain, respectively fasm and a graph library is what makes that raw language winning

1) at compile time
2) at run time
3) at portability time

and

once the semantics has been organized newly

4) at extensibility time.

Cheers,
Very Happy

_________________
⠓⠕⠏⠉⠕⠙⠑
Post 24 Oct 2013, 16:09
View user's profile Send private message Visit poster's website Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 6290
Location: ˛                              ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣ Posts: 6699
great potentials,
really great potentials,

i reserve my comments,
thanks uart777 for the gift to all human,
Post 24 Oct 2013, 18:37
View user's profile Send private message Reply with quote
uart777



Joined: 17 Jan 2012
Posts: 369
revolution: I was mainly a C programmer who only used ASM for little graphics demos (mode 13h/X/VESA) that are useless from a functional perspective.

Error checking? The library functions just return error code (usually 0) and the programmer can respond. I agree that LANGUAGE.INC should have more error checking (type safety), but it's not easy to get all macros synchronized on all levels throughout the entire library.

Compact code? I write code in lower resolution (1024x768) with bigger fonts (Consolas 16-18 bold on my I32 netbook connected to 32" HDTV). I hate having to hunch over and squint my eyes to see small text. This can eventually cause spinal damage/curvature.

Non-alphanumerics? It may appear as though my choice of symbols is completely random but there are reasons. Example: @ ('a'rray) is used for dynamic arrays temporarily until I can incorporate them in let (. p=array[i]) and function parameters (my.function array[i]).

hopcode: To me, the code is 100% extensible and customizable. I've never encountered a situation that requires OOP/classes. The one who proposed the Lisp syntax made it sound like OOP is the most common thing. From my experience, 90%+ of code is basic arithmetic (a=b+c, mov, add, etc). OOP is on my 2-DO list, just not the highest priority. Array/structure access is more important and a prerequisite to advance this language further. You are welcome to present alternative macros/syntaxes.

sleep: That's nice of you. If I get time, my next contributions may be programs to create visual documents/tutorials and share information.
Post 26 Oct 2013, 00:25
View user's profile Send private message Reply with quote
TmX



Joined: 02 Mar 2006
Posts: 766
Location: Jakarta, Indonesia
Hi uart,

What about the manual?
I must admit your language kinda look confusing (dots everywhere), although I can see some C influences in it.
Or maybe I'm missing something?
Post 26 Oct 2013, 17:21
View user's profile Send private message Reply with quote
uart777



Joined: 17 Jan 2012
Posts: 369
TmX: Hi. You asked about documentation before and I didn't have time to explain. 1. LANGUAGE.INC is commented. 2. Study the examples. 3. I assume that programmers know how to copy and paste macros from LANGUAGE.INC. At the minimum, they should know how to reuse existing macros. 4. This code is not for everyone. My target audience is bare-metal, embedded, OS creators. If I get time today, I'll post a standard Window in this thread that only includes LANGUAGE.INC+minimal.

Regarding dots...

* Assemblers generally prefix runtime macros with . - .if, .else, .while, etc
* let keyword was renamed to dot (.) to follow the tradition. There's only one dot in the language for all arithmetic. You're referring to the library names or local labels which are a feature of FASM
* Dot is the separator between words (draw.image) instead of the underscore _ which is hard (or impossible) to type on some keyboards. On every keyboard I've used, . and ? are always easy access (right ring finger down). Some symbols are reserved by FASM and can't be used in identifier names
* Mixed underscore and dots seems inconsistent: my_icon.image.x
* In FASM, labels that begin with a dot are local/attached to previous global label
* I get the impression that revolution is just making objective observations, pointing out differences, not necessarily saying good or bad. We can look at specific syntaxes and think of ways to improve them, but it's hard to judge the entire system with 1,200+ macros.

Let's just call this language Z or the macro language (please don't say uart's language). It's the only one we have now. Any questions about specific syntaxes? Ask. I can't explain all 1,200+ macros in one sentence or even one book.
Post 26 Oct 2013, 20:21
View user's profile Send private message Reply with quote
uart777



Joined: 17 Jan 2012
Posts: 369
Standard Window Example

Code:
include 'z.inc'

text title='My Window'wc.name='W1'

HANDLE modulewindow
MSG msg
WNDCLASSEX wc

;;;;;;;;;;;;;;;;;;;;;; MAIN ;;;;;;;;;;;;;;;;;;;;;

function main
 locals xywh

 get [module]=GetModuleHandleA 0

 . [wc.cbSize]=WNDCLASSEX.$
 . [wc.lpfnWndProc]=!window.procedure
 . [wc.hInstance]=[module]
 . [wc.lpszClassName]=wc.name
 . [wc.style]=CS_DBLCLKS
 . [wc.hbrBackground]=8

 get [wc.hIcon]=LoadIconA 07F00h
 get [wc.hCursor]=LoadCursorA 07F00h

 try RegisterClassExA wc

 . w=720h=480
 . r0=[os.w], r0>>1         ; x=sw/2-w/2
 . r2=wr2>>1r0-r2x=r0
 . r0=[os.h], r0>>1         ; y=sh/2-h/2
 . r2=hr2>>1r0-r2y=r0

 try [window]=CreateWindowExA 0wc.name,\
  titleWS_DEFAULTxywh,\
  00, [module], 0

 @@:
  GetMessageA msg000
  fail .r
  TranslateMessage msg
  DispatchMessageA msg
 jmp @b
 .r:
endf [msg.wParam]

;;;;;;;;;;;;;;;; WINDOW PROCEDURE ;;;;;;;;;;;;;;;;

function window.procedurewmessagewplp

 if message=WM_KEYDOWN
  say 'Key Press'

 else.if message=WM_LBUTTONDOWN
  say 'Mouse Click'

 else.if message=WM_CLOSE
  PostQuitMessage 0
  return 1
 end

 DefWindowProcA wmessagewplp
endf

Post 27 Oct 2013, 02:15
View user's profile Send private message Reply with quote
Sasha



Joined: 17 Nov 2011
Posts: 93

Quote:

I must admit your language kinda look confusing


I agree.
But some points are looking greate.
try RegisterClassExA wc
and what happens on error ? No "catch" ?
Do I need to write " try say 'Hello' "?

As someone could understand, I'm digging in the direction of my own language creation, too. Very slow. When looking at your work, I can't imagine how much time it took for you? I tried different approaches. Using fasm macro, and writing from scratch. Here is my macro attempts, and here I'm talking about parser. There is also an error handler in work.
Post 27 Oct 2013, 17:16
View user's profile Send private message Reply with quote
uart777



Joined: 17 Jan 2012
Posts: 369
Sasha: Your code is good and clear. It takes a long time to learn.

On cell phone. Real quick... (Motorola Photon=Best QWERTY phone):

First, all syntaxes are defined in LANGUAGE.INC (and some in ARRAY.INC temporarily).

. (aka, let) is for assignments and arithmetic.

get calls a function then assigns its return value:

Code:
get c=rgb 7Fh07Fh

try calls a function then returns zero (or error code) if it does: call foo / test eax, eax / jz endf. It can assign a variable like get. The equivelant in C/C++ is too much to type on this keyboard! In the following, create+allocate are functions.

Code:
try create file
try p=allocate 4*KB

fault sets the label for try on failure to perform some de-initiation before the return (deallocate memory, close files). Each function resets this to return/endf in the beginning.

Code:
fault .0 ; try will jmp to .0 if error

A question that has been echoing in mind:

Quote:
When is [] needed?

Only for global [memory]/value/integer/pointer/handle and [r] addressing modes. In next release, [] will be automatic for integral values as in Magic-Compiler. Not for address of global text or structures. Not for values of locals/parameters. Use & for address of locals/parameters as in C: . p=&v.
Post 27 Oct 2013, 23:49
View user's profile Send private message Reply with quote
Sasha



Joined: 17 Nov 2011
Posts: 93

Quote:

It takes a long time to learn.


How much time did it took for you to design and implement your language?
I'm learning from scratch! No programming background. Not a first year. After trying a different languages, I always go back to assembler...
Post 28 Oct 2013, 02:08
View user's profile Send private message Reply with quote
uart777



Joined: 17 Jan 2012
Posts: 369
You want the long answer?

89' - Introduced to programming on C64 BASIC. Playing with PRINT, INPUT, PSET. 94'-97' - First PC with DOS+Win3.1. Started programming seriously in BC 3.1 C/C++ and inline ASM+TASM. 97'-99'- MASM+VC++. Making games/graphics/utilities. 00'-09' - FASM+VC++, studying compilers, disassemblers, learned how to convert HL syntaxes (everything from bit fields to multi-dimensional array access), generating instruction tables, expression parsers, designing syntaxes, imaginary CPUs, making toy compilers, mini assemblers, executable formats. 09'-Now - Writing macros, languages, libraries.

Programming was always something I did in my freetime whenever I had shelter and electricity.
Post 28 Oct 2013, 08:40
View user's profile Send private message Reply with quote
Sasha



Joined: 17 Nov 2011
Posts: 93
Once I found an esolangs.org and spent there a lot of time. All you are writing about I was thinking about it, but still haven't made something that could be shown. I even went deeper and started to learn fpga's and logic circuits. Not long time ago I've found this impressive article and begun to believe in possibility of creation my own processor.
Post 28 Oct 2013, 14:59
View user's profile Send private message Reply with quote
uart777



Joined: 17 Jan 2012
Posts: 369
Sasha: You can find programs online to design integrated circuits with logic gates.

...

Finally got external USB modem ($5 used) and obtained more WIFI passwords from unsuspecting neighbors. Now, I can submit code.

Z, The Final Language. Easiest macro language ever created: Old-school simplicity and insane mental power. No language compares. Z gives one programmer the power to replace big teams, end careers overnight, put big corporations out of business (who think I'm not qualified to mop their floors, they better hope and pray that I never have money). Implementation will be a lifetime project, never finished. Example:

Code:
function main
 create f     ; create file
 write sn   ; write to current file
 close        ; close after create
 open f       ; reopen
 read tn    ; read to t
 say t        ; display text
 close        ; close after open
 execute f    ; execute maximize
endf

COMPARE: Z VS C/C++ VS ASM

Z...

Code:
try p=allocate 4*KB
try open name
read p4*KB
close


Code:
try p=load.file name ; or entire file

C/C++ Standard...

Code:
FILE *fp;
char *p;

if (!(p=(char*)malloc(4096))) {
  return 0;
}
if (!(fp=fopen(name"rb"))) {
  return 0;
}
fread(p14096fp);
fclose(fp);


"Standard"/So-Called "Assembler". Strictly X86/Windoze Specific. Pointless stdcall/invoke prefixes. What's wrong with the word "call"? Why change it for different CPUs+OSs? Writing code this way teaches NOTHING about CPU/assembler

Code:
invoke HeapAlloc, [_heap], 04096
mov [p], eax
test eaxeax
jz .r
invoke CreateFileAnameGENERIC_READ \
 or GENERIC_WRITEFILE_SHARE_READ0, \
 OPEN_EXISTING00
mov [fp], eax
cmp eax, -1
je .r
invoke ReadFile, [fp], [p], 4096tmp.rw0
invoke CloseHandle, [fp]

Smart Assembler. Written in a general, portable fashion, old-school style, prepared for translation to any CPU/OS. Which one is clearer? This or the previous? THINK for yourself.

Code:
call allocate4096
mov [p], r0
test r0r0
jz .r
call openname
mov [fp], r0
cmp r0, -1
je .r
call read, [p], 4096
call close



COMPARE: Z VS STANDARD

VARIABLES

Z: Multiple variables may be created on the same line with optional initial value (=) or 0.

Code:
; Z: 1 line...

integer xywhijnpqs

Standard: Variables are written on separate lines:

Code:
; Standard: 10 lines, 10 times as much code!

x dd 0
y dd 0
w dd 0
h dd 0
i dd 0
j dd 0
n dd 0
p dd 0
q dd 0
s dd 0

STRUCTURES

Z: Straight forward, plain English. 3-5+ members or instances may be written on the same line and optional ? virtual structure after (Zero-based offsets. Add ?s.m to a base register that contains the address of a structure).

Code:
; Z: 4 lines

structure IMAGE
 void p
 integer xywhbppkeyalpha
ends ?image

Standard: Members and instances must be written on separate lines. Virtual structures must be created manually for each type! (virtual at 0 ... end virtual).

Code:
; Standard: 21 lines, 5+ times as much code...

struct IMAGE
 p dd 0
 x dd 0
 y dd 0
 w dd 0
 h dd 0
 bpp dd 0
 key dd 0
 alpha dd 0
ends

virtual at 0
?image:
 .p dd 0
 .x dd 0
 .y dd 0
 .w dd 0
 .h dd 0
 .bpp dd 0
 .key dd 0
 .alpha dd 0
end virtual

Post 31 Oct 2013, 19:47
View user's profile Send private message Reply with quote
filox



Joined: 13 Feb 2013
Posts: 9
Hi UART,
can u make an example of simple socket server?

Otherwise very cool lib!!!
Post 09 Sep 2016, 11:36
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >

Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.