flat assembler
Message board for the users of flat assembler.

Index > Tutorials and Examples > New Z Language+Library + 10 Examples

Author
Thread Post new topic Reply to topic
uart777



Joined: 17 Jan 2012
Posts: 369
uart777 24 Oct 2013, 14:22
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: 19886 Time(s)

z.jpg


Description:
Download
Filename: !z.zip
Filesize: 390.15 KB
Downloaded: 1034 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
uart777 24 Oct 2013, 14:24
Preview/Excerpts:
Code:
; n+(((p-1)-(n+p-1))&(p-1))

function align.n, n, p
 . r1=p, r1-1, r2=n, r2+r1, r0=r1,\
 r0-r2, r0&r1, r1=r0, r2=n, r0+r2
endf  

macro set.nibble v, i, n { ; v|=(n<<(i*4))
 . r0=v, r1=i, r2=n, r1<<2, r2<<cl,\
 r0|r2, v=r0
}     

function triplet, a, b, c
 . r0=a, r0<<6, r1=b, r1<<3, r0|r1, r0|c
endf 

function rgb, r, g, b
 . r0=r, r0<<16, r1=g, r1<<8,\
 r0|r1, r2=b, r0|r2
endf  

; standard copy with 0 after

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

; attach b to a

function text.attach, a, b
 text.n a
 . r0+a
 text.copy r0, b
endf

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

function search.n, a, v, n
 . r0=a, r1=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.randomize, a
 locals i, j, n, s, max, p
 . r0=a, s=[?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 a, i, j, p
 endl
 destroy p
endf

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

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

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

function rotate.image, image, way
 locals x, y, w, h, p, s, col, row
 . r0=image, s=[?image.p+r0],\
  w=[?image.w+r0], h=[?image.h+r0]
 try p=scratch h, w ; w/h reversed
 if way='r'         ; right, 90
  . r0=h,\
   r0--, col=r0
  loop y=0 to h
   loop x=0 to w
    . r0=x, r0*h, r0+col, r0<<2,\
     r0+p, r2=y, r2*w, r2+x, r2<<2,\
    r2+s, [r0]=[r2]
   endl
   . col--
  endl
 else.if way='l' ; left, 270
  . col=0
  loop y=0 to h
   . r0=w, r0--, row=r0
   loop x=0 to w
    . r0=row, r0*h, r0+col, r0<<2,\
     r0+p, r2=y, r2*w, r2+x, r2<<2,\
    r2+s, [r0]=[r2], row--
   endl
   . col++
  endl
 end
 end.scratch image
 . r0=image,\ ; adjust location
  r2=[?image.x+r0], r1=w, r1>>1, r2+r1,\
  r1=h, r1>>1, r2-r1, [?image.x+r0]=r2,\
  r2=[?image.y+r0], r1=h, r1>>1, r2+r1,\
  r1=w, r1>>1, r2-r1, [?image.y+r0]=r2
endf     

; size by percentage (integer)

function size.image.p, image, p
 locals w, h, iw, ih
 . r0=image,\
  iw=[?image.w+r0], ih=[?image.h+r0],\
  r0=iw, r0*p, r1=100, r0/r1, w=r0,\
  r0=ih, r0*p, r1=100, r0/r1, h=r0
 size.image image, w, h
endf   

function mix, a, b, n
 locals sr, sg, sb, dr, dg, db
 . r0=a, r2=b

 . \ ; extract r/g/b components
  r1=r0, r1>>16, r1&0FFh, sr=r1,\
  r1=r0, r1>>8, r1&0FFh, sg=r1,\
  r1=r0, r1&0FFh, sb=r1,\
  r1=r2, r1>>16, r1&0FFh, dr=r1,\
  r1=r2, r1>>8, r1&0FFh, dg=r1,\
  r1=r2, r1&0FFh, db=r1

 . \ ; calculate deltas: (((s-d)*a)/256)+d
  r0=sr, r0-dr, r0*n, r0>>8, r0+dr,\
  r1=sg, r1-dg, r1*n, r1>>8, r1+dg,\
  r2=sb, r2-db, r2*n, r2>>8, r2+db

 . \ ; construct RGB
  r0<<16, r1<<8, r0|r1, r0|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.a, pixels, x, y, w
 locals i
 push r6 r7
 . r0=&i, r6=&x, r7=&y, r1=&w
 clip.scanline r0, r6, r7, r1
 fail .e
 vga.xy [r6], [r7]
 . r7=r0, r6=pixels, r6+i

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

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

function save.file, file, p, n
 fault .0
 try create file
 try write p, n
 close
 return 1
 .0: close
endf 0

function load.text, file
 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
 .0: flush
 close
endf 0

function save.text, file, t
 text.n t
 save.file file, t, r0
endf

function append.file, file, p, n
 fault .0
 !if open, file
  seek.r 0, SEEK_END
 else
  try create file
 end
 try write p, n
 close
 return file
 .0: close
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: 20423
Location: In your JS exploiting you and your system
revolution 24 Oct 2013, 14:39
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
hopcode 24 Oct 2013, 16:09
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: 12935
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 24 Oct 2013, 18:37
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
uart777 26 Oct 2013, 00:25
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: 843
Location: Jakarta, Indonesia
TmX 26 Oct 2013, 17:21
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
uart777 26 Oct 2013, 20:21
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
uart777 27 Oct 2013, 02:15
Standard Window Example
Code:
include 'z.inc'

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

HANDLE module, window
MSG msg
WNDCLASSEX wc

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

function main
 locals x, y, w, h

 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 0, 7F00h
 get [wc.hCursor]=LoadCursorA 0, 7F00h

 try RegisterClassExA wc

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

 try [window]=CreateWindowExA 0, wc.name,\
  title, WS_DEFAULT, x, y, w, h,\
  0, 0, [module], 0

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

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

function window.procedure, w, message, wp, lp

 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 w, message, wp, lp
endf    
Post 27 Oct 2013, 02:15
View user's profile Send private message Reply with quote
Sasha



Joined: 17 Nov 2011
Posts: 93
Sasha 27 Oct 2013, 17:16
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
uart777 27 Oct 2013, 23:49
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 7Fh, 0, 7Fh    
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
Sasha 28 Oct 2013, 02:08
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
uart777 28 Oct 2013, 08:40
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
Sasha 28 Oct 2013, 14:59
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
uart777 31 Oct 2013, 19:47
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 s, n   ; write to current file
 close        ; close after create
 open f       ; reopen
 read t, n    ; 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 p, 4*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(p, 1, 4096, fp);
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], 0, 4096
mov [p], eax
test eax, eax
jz .r
invoke CreateFileA, name, GENERIC_READ \
 or GENERIC_WRITE, FILE_SHARE_READ, 0, \
 OPEN_EXISTING, 0, 0
mov [fp], eax
cmp eax, -1
je .r
invoke ReadFile, [fp], [p], 4096, tmp.rw, 0
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 allocate, 4096
mov [p], r0
test r0, r0
jz .r
call open, name
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 x, y, w, h, i, j, n, p, q, s    
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 x, y, w, h, bpp, key, alpha
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: 11
filox 09 Sep 2016, 11:36
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


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.