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

daydreamer

  • Member
  • ****
  • Posts: 944
  • watch Chebyshev on the backside of the Moon
do you not approve of gotoswhy?do you instead like jumptables?
« on: September 05, 2019, 12:15:28 AM »
do you belong to those who think gotos do not belong in programming?why?why not when your HLL code maybe will be  compiled into jumps and calls?
or do you like jumptables instead?why?and jumps instead of stick to invokes and proc's?
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 #1 on: September 05, 2019, 12:47:20 AM »
I like to use jump tables in my code when it needs to be fast and it can minimize code size.
Creative coders use backward thinking techniques as a strategy.

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 #2 on: September 05, 2019, 12:48:49 AM »
The people who sprout this bullsh*t were not old enough to have written code that required it long long ago. Branching is an integral part of decision making, many high level methods work OK until you have to do something complicated then you end up with ugly inefficient code. Using branching where it is needed, the alternative is cluttered junk.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

AW

  • Member
  • *****
  • Posts: 2442
  • Let's Make ASM Great Again!
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #3 on: September 05, 2019, 01:58:14 AM »
This was mostly for C and other HLL, not ASM.
Even there, some authors like Knuth consider that in some cases a "goto" is the optimal construct. And even when not explicitly using the "goto" language instructions, instructions like "break" or "continue" are indeed "gotos" in disguise.
I have here "C" code from Microsoft where "gotos" are used without any inhibition.
Anyway, if we are doing structured programming we will not feel the need to use "goto" many times, but if it sounds like the best option I don't think we should go to great lengths to avoid 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 #4 on: September 05, 2019, 04:36:43 AM »
I am trying different ways of solving exercises and was wondering how everyone else felt about it
because some both do assembly and C/C++ coding
Quote from Flashdance
Nick  :  When you give up your dream, you die
*wears a flameproof asbestos suit*
Gone serverside programming p:  :D

caballero

  • Member
  • *****
  • Posts: 1215
  • Matrix - Noah
    • abre ojos ensamblador
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #5 on: September 05, 2019, 05:48:26 AM »
In general, you should avoid gotos, otherwise you will find many difficulties to read your own code after a few days. Particularly, I find awful using gotos where subroutines should go.
The logic of the error is hidden among the most unexpected lines of the program

HSE

  • Member
  • *****
  • Posts: 1148
  • <AMD>< 7-32>
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #6 on: September 05, 2019, 06:09:45 AM »
In general, you should avoid gotos, otherwise you will find many difficulties to read your own code after a few days.
I think, it's impossible to read my own code after a few days because it's splitted in a dozen of files  :biggrin: :biggrin: :biggrin:

Particularly, I find awful using gotos where subroutines should go.
and it's awful to left a subroutine that can be replace by an inclusion macro (contained in another file, of course) :joking:

caballero

  • Member
  • *****
  • Posts: 1215
  • Matrix - Noah
    • abre ojos ensamblador
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #7 on: September 05, 2019, 06:28:05 AM »
It's a matter of taste, I guess :thumbsup:, I would only use macros with a few lines and used as few as possible because macros expanded itself and it is hard to debug and increases the exe size. For example:
Code: [Select]
SetMode MACRO Modo
  ; Propósito : Establecer el módo gráfico/texto
  ; Entrada   : Ninguna
  ; Salida    : Salida
  ; Destruye  : AX
  ; Coment    : 13h: 320x200x256, 3h: modo texto 80x25x16
  MOV     AH, 0h
  MOV     AL, Modo                      ; Modo 13h, 320x200x256
  INT     10h                           ; Lo hacemos
ENDM
The logic of the error is hidden among the most unexpected lines of the program

felipe

  • Member
  • *****
  • Posts: 1249
  • Eagles are just great!
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #8 on: September 05, 2019, 07:06:03 AM »
increases the exe size.

Why a macro (masm macros talking here) would increase the exe's size?
Felipe.

jj2007

  • Member
  • *****
  • Posts: 9804
  • Assembler is fun ;-)
    • MasmBasic
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #9 on: September 05, 2019, 07:15:51 AM »
increases the exe size.

Why a macro (masm macros talking here) would increase the exe's size?

Good question, Felipe :thumbsup:

HSE

  • Member
  • *****
  • Posts: 1148
  • <AMD>< 7-32>
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #10 on: September 05, 2019, 07:56:55 AM »
Why a macro (masm macros talking here) would increase the exe's size?

It's a pretty old discussion about to call a subrutine or to expand the same code again. But I'm not refering to that, but to inclusion macros that are used like a sniplet:
Code: [Select]
a1 macro
   ...
endm
a2 macro
   ....
end
a3 macro
   ...
endm

.if ....
    a1
.elseif ...
    a2
.else
   a3
.endif

jj2007

  • Member
  • *****
  • Posts: 9804
  • Assembler is fun ;-)
    • MasmBasic
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #11 on: September 05, 2019, 03:47:33 PM »
Macros don't increase the exe size, unless you do something utterly wrong. But there are also people who believe that a .Repeat ... .Until loop or a .if ... .else ... .endif construct are, for some magical reason, longer than their "pure metal" code. The reason for such a belief is that they a) never tried and b) never debugged a macro.

TimoVJL

  • Member
  • ***
  • Posts: 476
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #12 on: September 05, 2019, 04:32:21 PM »
Macros don't increase the exe size, unless you do something utterly wrong.
like call a WRONG macro :undecided:
May the source be with you

AW

  • Member
  • *****
  • Posts: 2442
  • Let's Make ASM Great Again!
Re: do you not approve of gotoswhy?do you instead like jumptables?
« Reply #13 on: September 05, 2019, 04:39:38 PM »
An advantage of macros is that we can always blame Microsoft when they don't work and are unable to find the reason.
A macro addicted can blame but will never report a bug to Microsoft because they don't know how to do it without using macros.

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 #14 on: September 05, 2019, 06:19:08 PM »
Problems with macros come from bad design, each time you use a macro you add code so in the context of a sequence of macros, you have repeated the code insertion many times and while it may work, its BADC0DEh.

Now here is what at least some will call a JMP table.

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

    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, 10
    jl lbl

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

    conout lf

    waitkey
    RestoreRegs
    .exit

 entry_point endp

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

WordTable proc

    .data
      one db "one",0                                    ; the data
      two db "two",0
      three db "three",0
      four db "four",0
      five db "five",0
      six db "six",0
      seven db "seven",0
      eight db "eight",0
      wtbl dq one,two,three,four,five,six,seven,eight   ; the table
    .code

    cmp rcx, 8                                          ; set top limit
    jg error
    test rcx, rcx                                       ; set bottom limit
    jz error

    lea rdx, wtbl                                       ; load table address into rdx
    mov rax, QWORD PTR [rdx+rcx*8-8]                    ; store data at address into rax
    ret

  error:
    xor rax, rax
    ret

WordTable endp

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

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