4 Replies - 849 Views - Last Post: 07 June 2019 - 03:41 AM

#1 CVRIV   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 03-February 18

CALL vs. GOTO, which to use and when?

Posted 06 June 2019 - 03:57 AM

When I was taking a class in microcontrollers, I used GOTO and never really used CALL. My professor said I use GOTO way to much, basically. He said I need to use CALL.

When I was taking my class in C++, I did a hard 180 and starting using CALL because using GOTO is frowned upon, at least in C++.

My question is, when to use CALL vs GOTO? I'm guessing that GOTO is used to create loops and CALL is used to organize everything in subroutines. Is this right or am I missing something?

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: CALL vs. GOTO, which to use and when?

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2414
  • View blog
  • Posts: 4,546
  • Joined: 30-May 10

Re: CALL vs. GOTO, which to use and when?

Posted 06 June 2019 - 10:50 PM

> I'm guessing that GOTO is used to create loops and CALL is used to organize everything in subroutines.
> Is this right or am I missing something?
Yes, that's right.

You write a sub-routine and CALL it when you identify so functionality that can be re-used.

GOTO is used (and abused) for all sorts of things, including loops.
Some assemblers have features to help with writing loops, but if you need to do it all yourself, consistency of naming helps.
for ( i = 0 ; i < 10 ; i++ ) {
    write(i);
}



Would be something like
f1_init:  ;; The init part of a loop
    mov  r0,0
f1_loop:  ;; The test part of a loop
    cmp  r0,10
    bge  f1_done
f1_body:  ;; The body of a loop
    push r0
    call write
    pop  r0
f1_step:  ;; The step part of a loop
    inc  r0
    jmp  f1_loop
f1_done:


Was This Post Helpful? 0
  • +
  • -

#3 CVRIV   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 03-February 18

Re: CALL vs. GOTO, which to use and when?

Posted 07 June 2019 - 02:47 AM

I'm am trying to refresh myself with all this. It's been more than a year, almost two years since I've done this. I do remember seeing some more high level language type stuff that could be used like for loops etc. I think they were referred to as directives? Or maybe that's something different? Anywho... my professor wanted us to focus on the instructions only, but by the end of the semester I had started using more and more directives. I wasn't using for loops etc. I was using GOTO to build loops and everything really.

I took a C++ class two semesters ago and loved it. Eventually I want to get into coding PIC controllers with C++ but for now I want to focus on assembly etc.

So using GOTO to create loops is frowned upon?
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2414
  • View blog
  • Posts: 4,546
  • Joined: 30-May 10

Re: CALL vs. GOTO, which to use and when?

Posted 07 June 2019 - 03:25 AM

Well if your assembler lacks directives, macros, templates or whatever you like to call them to create your loops, then you have no choice but to use jump/branch instructions of one sort or another.
Was This Post Helpful? 0
  • +
  • -

#5 CVRIV   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 03-February 18

Re: CALL vs. GOTO, which to use and when?

Posted 07 June 2019 - 03:41 AM

View PostSalem_c, on 07 June 2019 - 03:25 AM, said:

Well if your assembler lacks directives, macros, templates or whatever you like to call them to create your loops, then you have no choice but to use jump/branch instructions of one sort or another.


Gotcha. I'm using MPLAB 8. I really need to start using the latest version too.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1