[Oberon] INC(i) vs i := i + 1

Joerg joerg.straube at iaeth.ch
Wed May 5 10:29:48 CEST 2021



Just as an example, find below the output of the RISC5 compiler:


The statements “i := i+1;” and “INC(i);” generate exactly the same output, namely

  LDW R0 SP 4      load the local variable “i” (is at position 4 on the stack)

  ADD R0 R0 1      add 1

  STW R0 SP 4      store register to local variable


The statement “a[n] := a[n] + 1;” generates

  ; address of a[n] for LHS

  LDW R0 SP 8        load “n” (at position 8 on stack)

  SUB R1 R0 30      check array limit (in my case ARRAY 30 OF INTEGER)

  BLHI MT                trap if outside
  LSL R0 R0 2           multiply “n” with size of INTEGER
  ADD R0 SP R0       add frame pointer

  ; address a[n] for RHS

  LDW R1 SP 8        load “n” (at position 8 on stack)

  SUB R2 R1 30       compare with upper limit of array

  BLHI MT                trap if outside
  LSL R1 R1 2           multiply “n” with size of INTEGER
  ADD R1 SP R1       add frame pointer

  ; add and store

  LDW R1 R1 12      12 is offset of the array “a”

  ADD R1 R1 1

  STW R1 R0 12


An optimizing compiler could detect, that ADR(a[n]) is already in R0 and could skip the second calculation for R1. But NW’s compiler does not do that.


In contrast “INC(a[n]);” is a little shorter and hence faster, as you have to calculate the address only once

  ; address of a[n]

  LDW R0 SP 8

  SUB R1 R0 30


  LSL R0 R0 2

  ADD R0 SP R0

  ADD R0 R0 12   offset of a

  ; add and store

  LDW R1 R0 0

  ADD R1 R1 1

  STW R1 R0 0





Von: Oberon <oberon-bounces at lists.inf.ethz.ch> im Auftrag von Jeff Maggio <jmaggio14 at gmail.com>
Antworten an: ETH Oberon and related systems <oberon at lists.inf.ethz.ch>
Datum: Dienstag, 4. Mai 2021 um 23:00
An: <oberon at lists.inf.ethz.ch>
Betreff: [Oberon] INC(i) vs i := i + 1


Yeah, for our application I want to write very explicit code that's fairly self-explanatory as part of a tutorial so I chose i := i + 1. Even if they end up using INC(i) down the road, INC(i) isn't necessarily self-explanatory for a novice user without assembly experience (like me for example). 


It's a small consideration though - I was mostly curious if they are compiled differently




My understanding is that if an experienced Python programmer just wanted to increment an integer variable they wouldn't write i = i + 1, instead they would write:
  i += 1
Similarly C++ programmers would write
INC(i) is the best match in Oberon for either of these statements.
I suspect they would be unhappy if you told them to rewrite this in Python / C++ as
  i = i + 1
so they would see this as a disadvantage if they were told that they had to write it this way in Oberon.
Chris Burrows
CFB Software
-- Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems https://lists.inf.ethz.ch/mailman/listinfo/oberon 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.inf.ethz.ch/pipermail/oberon/attachments/20210505/372aaee9/attachment.html>

More information about the Oberon mailing list