programming tricks

8 posts / 0 new
Last post
jwanderson88
jwanderson88's picture
Offline
Last seen: 3 weeks 10 hours ago
Joined: 2019-04-13 19:29
programming tricks

I posted a few days ago that I was exploring disassembling. I'm working on a 6502 disassembler now. I'm attempting to adapt programs written in C by other programmers years ago written for the Amiga. They are rife with programming tricks. For example, this one:

for (address1 = sizeof MemType / sizeof *MemType; address1--;
MemType[address1] = 0);

I can't tell what this does.

This is the syntax of a for loop:

Syntax
The syntax of a for loop in C programming language is −

for ( init; condition; increment ) {
statement(s);
}

This forum is populated with accomplished, experienced programmers. Maybe somebody can tell me why programmers use tricks to force functions to do things they weren't meant to do. Are they just showing off?

salass00
salass00's picture
Offline
Last seen: 2 days 16 hours ago
Joined: 2011-02-03 11:27
Re: programming tricks

It is exactly the same as:

  1. address1 = sizeof(MemType) / sizeof(*MemType);
  2. while (address1--)
  3. {
  4. MemType[address1] = 0;
  5. }

The content of a for loop in C (as with other similar constructs) can be either a single statement or a statement block delineated by curly braces. In your case it was just an empty statement.

jwanderson88
jwanderson88's picture
Offline
Last seen: 3 weeks 10 hours ago
Joined: 2019-04-13 19:29
Re: programming tricks

Thanks, salass00, your code is very understandable at a glance. That's my whole point. Now that I look at it again, I can see that it will keep doing whatever is in the 3rd parameter as long as address in the second parameter is positive. The empty "body" is not what is confusing.

msteed
msteed's picture
Offline
Last seen: 2 weeks 5 days ago
Joined: 2022-01-18 08:34
Re: programming tricks

If you haven't already figured it out, the code construct sizeof(<entire array>) / sizeof(<one element of array>) is a common way of determining the number of elements in the array at compile time. If the number of elements is changed this code picks up the new value automatically; you don't need to worry about forgetting to adjust a loop limit somewhere in your code.

While there are no doubt programmers who revel in using obscure constructs in their code (witness the Obfuscated C Code contest), I'm not sure that either this or body-less for loops are really such tricks; they're just advanced coding techniques that one picks up over time. And now you know them, too!

LyleHaze
LyleHaze's picture
Offline
Last seen: 3 weeks 2 days ago
Joined: 2011-05-26 03:58
Re: programming tricks

From a more general point of view:
When I was young and learning programming, I thought the "tricks" were proof that I was smart.
As I got better, I could read the shortcuts as quickly as the code written "normally".
Then I became a teacher, and I understood that writing it out as simply and clearly as I could
would make it easier on others to understand what I wrote. "Others" includes me after a couple
years to forget what I was doing.

I'm not "locked in" to an unchanging syntax. I try to keep an open mind for useful changes, but
by and large I try to make it as readable (and with LOTS of comments) as possible. Some more
experienced coders might not like all my choices, but at least I try to make it readable.

Lyle

LyleHaze
jwanderson88
jwanderson88's picture
Offline
Last seen: 3 weeks 10 hours ago
Joined: 2019-04-13 19:29
Re: programming tricks

I want to correct something I said in my last post. The for loop will execute as long as parameter two is not 0. It doesn't have to be positive.

jwanderson88
jwanderson88's picture
Offline
Last seen: 3 weeks 10 hours ago
Joined: 2019-04-13 19:29
Re: programming tricks

I have whined about obscure programming, so I decided I should give some credit. The program I'm adapting is D65 from aminet if anyone is interested. After I got it to run without crashing, I ran it on a simple example program and got the following results. Frankly I was completely astounded. It ran, it created labels, it's good. I thought I would have more trouble. Next I'd like to try compiling it, but I'll have to find a compiler. Back in the 1980's, I wrote an entire game in 6502 assembler for the C64. It seems like I made a printout, but I can't find it. Oh, the good old days. You have to be real obsessed to write in assembler. The only significant problem I had was that MS Visual C++ couldn't find the end of some for loops. She's the gold standard. You don't want to get her upset. I have an ulterior motive for needing a disassembler. I'd like to disassemble the game called "M.U.L.E.". I imagine it's been attempted before., The original text below had the output neatly formatted in columns. The browser took all the extra spaces out or something.

.ORG $1000
1000 00 0B 10 .BYTE $0,$B,$10
1003 0A 00 9E .BYTE $A,$0,$9E
1006 34 31 31 .BYTE $34,$31,$31
1009 32 00 00 .BYTE $32,$0,$0
100C 00 00 00 .BYTE $0,$0,$0
100F 00 .BYTE $0
1010 L1010:
1010 AD 19 FF LDA $FF19
1013 A2 00 LDX #$0
1015 20 21 10 JSR L1021
1018 A2 80 LDX #$80
101A 20 21 10 JSR L1021
101D 8D 19 FF STA $FF19
1020 60 RTS
1021 L1021:
1021 8E 19 FF STX $FF19
1024 20 2B 10 JSR L102B
1027 E8 INX
1028 D0 F7 BNE L1021
102A L102A:
102A 60 RTS
102B L102B:
102B A0 00 LDY #$0
102D L102D:
102D C8 INY
102E D0 FD BNE L102D
1030 L1030:
1030 60 RTS

msteed
msteed's picture
Offline
Last seen: 2 weeks 5 days ago
Joined: 2022-01-18 08:34
Re: programming tricks
The original text below had the output neatly formatted in columns. The browser took all the extra spaces out or something.
HTML tends to eat whitespace. To keep that from happening, put [code] at the beginning of your block of code, and [/code] at the end.
Log in or register to post comments