Author Topic: do you not approve of gotoswhy?do you instead like jumptables?  (Read 2389 times)

TimoVJL

  • Member
  • ***
  • Posts: 476
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #15 on: September 05, 2019, 07:00:02 PM »
hutch's example in HLL / C
Code: [Select]
#pragma comment(lib, "msvcrt.lib")

char *WordTable(int nId);

void __cdecl mainCRTStartup(void)
{
char *pWord;
if (!(pWord = WordTable(0))) puts("out of range");
else puts(pWord);
}

char *WordTable(int nId)
{
static char *aWord[] = {"zero","one","two","three","four","five","six","seven","eight","nine","ten"};
//if (nId < 0 || nId > sizeof(aWord)/sizeof(aWord[0])) goto error;
if (nId >= 0 && nId < sizeof(aWord)/sizeof(aWord[0]))
return aWord[nId];
//error:
return 0;
}
'goto or not goto, that's the problem'
goto is often used in error handling to avoid making if blocks or while/break/return constructs.
« Last Edit: September 05, 2019, 08:41:32 PM by TimoVJL »
May the source be with you

jj2007

  • Member
  • *****
  • Posts: 9804
  • Assembler is fun ;-)
    • MasmBasic
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #16 on: September 05, 2019, 09:12:33 PM »
Similar, using Choose():
Code: [Select]
include \masm32\MasmBasic\MasmBasic.inc
  tx123 db "This is tx123", 0
  Init
  .While 1
Inkey "Gimme a number: "
.Break .if eax==VK_ESCAPE
.if Choose(eax-"0", 100, 101, 102, "abc", 124, 125, 12345.67, 12345678.90123456789, offset tx123)<=ChooseString
.if eax==ChooseReal
Print Str$("You chose the real number %Jf\n", ST(0)v) ; reals passed on fpu; trailing v: fstp st
.elseif eax==ChooseError
PrintLine "No such entry"
.else
Print Str$("You chose the integer %i\n", eax)
.endif
.else
PrintLine "You chose the string [", eax, "]"
.endif
  .Endw
EndOfCode

Output when typing 0...9:
Code: [Select]
Gimme a number: You chose the integer 100
Gimme a number: You chose the integer 101
Gimme a number: You chose the integer 102
Gimme a number: You chose the string [abc]
Gimme a number: You chose the integer 124
Gimme a number: You chose the integer 125
Gimme a number: You chose the real number 12345.67000000000000
Gimme a number: You chose the real number 12345678.90123456789
Gimme a number: You chose the string [This is tx123]
Gimme a number: No such entry

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 6769
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #17 on: September 05, 2019, 09:32:54 PM »
Here is the tidied up version. Five (5) instructions if number in range, 4 if not. The technique is suitable for large counts, hundreds to thousands and is subject to automation if it is useful.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include64\masm64rt.inc

    .code

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

 entry_point proc

    USING r12

    SaveRegs

  ; --------------------------------

    mov r12, 0
  lbl:
    .if rvcall(WordTable,r12) == 0
      conout "out of range",lf
    .else
      conout rax,lf
    .endif

    add r12, 1
    cmp r12, 12
    jl lbl

  ; --------------------------------

    conout lf

    waitkey
    RestoreRegs
    .exit

 entry_point endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

NOSTACKFRAME

WordTable proc

    .data
      @@_d0 db "zero",0                                 ; the data
      @@_d1 db "one",0
      @@_d2 db "two",0
      @@_d3 db "three",0
      @@_d4 db "four",0
      @@_d5 db "five",0
      @@_d6 db "six",0
      @@_d7 db "seven",0
      @@_d8 db "eight",0
      @@_d9 db "nine",0
      align 8
      wtbl$$$$ dq @@_d0,@@_d1,@@_d2,@@_d3,@@_d4, \
                  @@_d5,@@_d6,@@_d7,@@_d8,@@_d9         ; the table
    .code

    cmp rcx, 10                                         ; set array count
    jg error
    lea rdx, wtbl$$$$                                   ; load table address into rdx
    mov rax, QWORD PTR [rdx+rcx*8]                      ; store data address into rax
    ret

  error:
    xor rax, rax
    ret

WordTable endp

STACKFRAME

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    end
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

daydreamer

  • Member
  • ****
  • Posts: 944
  • watch Chebyshev on the backside of the Moon
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #18 on: September 07, 2019, 01:19:02 AM »
Good example hutch
I made menu with different choices and :
Fadd (ebx)
Ret
Fsub (ebx)
Ret
Fmul (ebx)
Ret
Fdiv (ebx)
Ret

Isnt it best way when writing an emulator?,or virtual machine? Processing a file of vector graphics ?
Siekmanski maybe used jmptable in a graphics program?
Quote from Flashdance
Nick  :  When you give up your dream, you die
*wears a flameproof asbestos suit*
Gone serverside programming p:  :D

Siekmanski

  • Member
  • *****
  • Posts: 1929
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #19 on: September 07, 2019, 04:45:00 AM »
Good example hutch
I made menu with different choices and :
Fadd (ebx)
Ret
Fsub (ebx)
Ret
Fmul (ebx)
Ret
Fdiv (ebx)
Ret

Isnt it best way when writing an emulator?,or virtual machine? Processing a file of vector graphics ?
Siekmanski maybe used jmptable in a graphics program?

Hi Magnus,

Yes, I've used jump tables in graphics-demos to call graphic routines from a time based storyboard.
And in music programming, reading the music note/effect data to jump to multiple sound and editor effect routines.
Creative coders use backward thinking techniques as a strategy.

FORTRANS

  • Member
  • *****
  • Posts: 1057
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #20 on: September 07, 2019, 04:54:59 AM »
Hi,

Isnt it best way when writing an emulator?,or virtual machine? Processing a file of vector graphics ?
Siekmanski maybe used jmptable in a graphics program?

   I have used jump tables to execute commands
from an input stream.  Each command is decoded
and the appropriate subroutine is called.  An example
command table:

Code: [Select]
; - - - Read Command - - -
; 6 April 2008
;ComByte DB      0
Command DW      ?
 ; A Null                       ; N ReadGCX:     #
 ; B WriteGCT:    #             ; O WriteAX:     #
 ; C SetTGAColor                ; P SetPacket:
 ; D Null; Decimal Input        ; Q InitStuff
 ; E ReadLSD:     #             ; R ReadLCT
 ; F SetFirst                   ; S SetSize
 ; G ReadGCT:     #             ; T TRAILER:     #
 ; H Null; Hexadecimal Input    ; U
 ; I WriteImDesc: #             ; V
 ; J ReadImDesc:  #             ; W WriteImage:  #
 ; K WriteLCT:                  ; X ExitCmd
 ; L WriteLSD:    #             ; Y
 ; M WriteGCX:    #             ; Z WriteBug
ComTable DW     Null,      WriteGCT,    SetTGAColor, Null,      ReadLSD
         DW     SetFirst,  ReadGCT,     Null,      WriteImDesc, ReadImDesc
         DW     WriteLCT,  WriteLSD,    WriteGCX,  ReadGCX,     WriteAX
         DW     SetPacket, InitStuff,   ReadLCT,      SetSize,     TRAILER
         DW     Null,      Null,    WriteImage,    ExitCmd,     Null
         DW     WriteBug
ComLabel DB     'Null A $', 'WritGCT$', 'SetTGAc$', 'Null D $', 'ReadLSD$'
         DB     'SetFirs$', 'ReadGCT$', 'Null H $', 'WrImDsc$', 'RdImDsc$'
         DB     'WritLCT$', 'WritLSD$', 'WritGCX$', 'ReadGCX$', 'WriteAX$'
         DB     'Packet $', 'InStuff$', 'ReadLCT$', 'SetSize$', 'TRAILER$'
         DB     'Null U $', 'Null V $', 'WrtImg $', 'ExitCmd$', 'Null Y $'
         DB     'WriteBug$'

Cheers,

Steve N.

daydreamer

  • Member
  • ****
  • Posts: 944
  • watch Chebyshev on the backside of the Moon
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #21 on: September 08, 2019, 10:28:51 PM »
Yes, I've used jump tables in graphics-demos to call graphic routines from a time based storyboard.
And in music programming, reading the music note/effect data to jump to multiple sound and editor effect routines.
combined with a multimedia timer?
why not floating point variable increment over time,increment less than 1.0 per frame
converted to integer used in jmptable
jmp init;initialize demo
jmp renderloop1 ;render tunneldemo X number of frames
jmp renderloop2 ;render fireworkdemo
jmp renderloop3 ;render xordemo
jmp renderloop4 ;render jumpgatedemo
...
@fortrans ,nice  :thumbsup:
Quote from Flashdance
Nick  :  When you give up your dream, you die
*wears a flameproof asbestos suit*
Gone serverside programming p:  :D

Siekmanski

  • Member
  • *****
  • Posts: 1929
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #22 on: September 09, 2019, 06:51:59 AM »
Yep, the multimedia timer runs with floating point values.
The storyboard routine handles the jump table offsets to the graph routines but also the time values and special effect parameters.
Creative coders use backward thinking techniques as a strategy.

xanatose

  • Member
  • ***
  • Posts: 387
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #23 on: September 09, 2019, 06:44:58 PM »
Every jump is a goto. thereof is impossible to program without a goto.
However is possible to hide the goto so that fools feel happy. a break; is a goto. A if/else has gotos. A switch has gotos. You get the idea.
Incidentally the same fools that believe that exceptions, which are far worse than goto, are a good idea. So I guess there is some cognitive dissonance going on.

At the end of day, goto, like a hammer, is a tool. Is up to you, the craftman,  to decide when to use it.

daydreamer

  • Member
  • ****
  • Posts: 944
  • watch Chebyshev on the backside of the Moon
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #24 on: September 11, 2019, 08:06:12 PM »
Every jump is a goto. thereof is impossible to program without a goto.
However is possible to hide the goto so that fools feel happy. a break; is a goto. A if/else has gotos. A switch has gotos. You get the idea.
Incidentally the same fools that believe that exceptions, which are far worse than goto, are a good idea. So I guess there is some cognitive dissonance going on.

At the end of day, goto, like a hammer, is a tool. Is up to you, the craftman,  to decide when to use it.
:thumbsup:
I agree,this is just what I meant in first post,even if you are programmer that avoid goto at all cost ,there will be gotos(jumps) after code been compiled

and if you call a single goto like a hammer,maybe jumptable should be like a sledgehammer :smiley:
Quote from Flashdance
Nick  :  When you give up your dream, you die
*wears a flameproof asbestos suit*
Gone serverside programming p:  :D

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 6769
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #25 on: September 11, 2019, 08:20:26 PM »
The logic is impossible without branching, either conditional or unconditional, you can have them wrapped in garbage but the bottom line is you cannot perform logic without branches. GOTO is just a wrapper for an unconditional JMP.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

xanatose

  • Member
  • ***
  • Posts: 387
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #26 on: October 23, 2019, 05:59:06 PM »
I do like jump tables. Besides looking more elegant than a wall of code. They offer the opportunity to change what function is called depending on processor.
Just change the pointer in the table before calling the functions. And you can take advantage of later processor functions without sacrificing support for older processors or having to compile a different exe for each processor. Or one path for a software solution and another path for specialized hardware.

Raistlin

  • Member
  • ****
  • Posts: 501
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #27 on: October 24, 2019, 04:53:35 PM »
This is highly interesting. It's been established that semantically, contextually and physically, gotos and jumps are equivalent in compiled code.
Lets visualize the jump table scenario. To ensure reasonable performance what conditions need to be satisfied ? (please lambaste & educate)

I'am again implying caching strategies may come into play and impacts on branch prediction must (should?) be measurable.
[alluded to here, in above posts]. Certainly profiling the bottle-necks in the code now becomes critical. Perhaps you
will be forced to use those pesky caching instructions we've all been avoiding. With some notoriety, the windows scheduler
could add unintended cycles in run-time scenarios, to over-compensate for the audacity of trying to manipulate the CPU's precognition.
Are you pondering what I'm pondering? It's time to take over the world ! - let's use ASSEMBLY...

jj2007

  • Member
  • *****
  • Posts: 9804
  • Assembler is fun ;-)
    • MasmBasic
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #28 on: October 24, 2019, 06:54:22 PM »
Lets visualize the jump table scenario. To ensure reasonable performance what conditions need to be satisfied ? (please lambaste & educate)

I thought we had beaten that to death in the High Level Language thread, 3 years ago :rolleyes:

xanatose

  • Member
  • ***
  • Posts: 387
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #29 on: October 24, 2019, 06:56:06 PM »
That's what benchmarks are for.  :biggrin:

But given that higher level languages do use virtual tables for inheritance of classes. And that most people use higher level languages.
I wouldn't be surprised if the impact is not as bad as most would think.

As always. you are the craftsman. Is up to use to know what tool to use and when to use it. There is no silver bullet. Jump tables are just another tool.  One that every Win32 program uses, as when you call a DLL function, you use a virtual table to do so.