←back to thread

The C23 edition of Modern C

(gustedt.wordpress.com)
515 points bwidlar | 4 comments | | HN request time: 2.178s | source
Show context
belter ◴[] No.41850897[source]
Important reminder just in the Preface :-)

Takeaway #1: "C and C++ are different: don’t mix them, and don’t mix them up"

replies(8): >>41850960 #>>41851047 #>>41851166 #>>41851693 #>>41853183 #>>41855660 #>>41857019 #>>41858537 #
jpcfl ◴[] No.41851047[source]
Bjarne should have called it ++C.
replies(2): >>41851328 #>>41854437 #
card_zero ◴[] No.41851328[source]
Because people choose to use pre-increment by default instead of post-increment?

Why is that?

replies(4): >>41851427 #>>41851557 #>>41851650 #>>41853498 #
wpollock ◴[] No.41853498[source]
The PDP-11 that C originally targeted had address modes to support the stack. Pre-increment and post-decrement therefore did not require a separate instruction; they were free. After the PDP-11 went the way of the dodo, both forms took a machine cycle so it (mostly) became a stylistic issue. (The two operators have different semantics, but the trend to avoid side-effects in expressions means that both are most often used in a single expression statement like "++x;" or "x++;", so it comes down to your preferred style.)
replies(1): >>41855124 #
zabzonk ◴[] No.41855124[source]
Please explain what you mean by "a separate instruction".
replies(2): >>41855741 #>>41864639 #
1. spc476 ◴[] No.41855741[source]
Some idiomatic C code to copy a string (I'm not saying this is good C code, but it's just an example):

    while(*d++ = *s++)
      ;
 
On the Motorola 68000 (based somewhat on the PDP-11) the code would look like:

    loop:       move.b  (a0)+,d0
                move.b  d0,(a1)+
                bne     loop
 
while on the x86 line, it would be:

    loop:       mov     al,[rsi]
                mov     [rdi],al
                inc     rsi     ; extra instruction!
                inc     rdi     ; extra instruction!
                cmp     al,0
                jne     loop
 
Yes, there are better ways to write that code for both the 68K and x86, but I hope this gets the point across.
replies(1): >>41860562 #
2. wang_li ◴[] No.41860562[source]
> loop: move.b (a0)+,d0 move.b d0,(a1)+

...

> loop: mov al,[rsi] mov [rdi],al

This hurts my brain. When we invent time machines I'm going to use it to go back and slap whoever at intel came up with that operand order.

replies(2): >>41876747 #>>41880372 #
3. spc476 ◴[] No.41876747[source]
It hurts less if you think of it like assignment:

    al = [rsi]
    [rdi] = al
4. int_19h ◴[] No.41880372[source]
memcpy etc also take the destination as their first argument, and it mirrors the usual way we write assignments. Personally I always found Intel syntax to be more straightforward, but I think it's ultimately down to whatever one was exposed to first.

I wonder sometimes why we keep insisting on the "OP ARG, ARG" format in general for assembly. Why not something like `MOV X -> Y` that would make it absolutely clear and unambiguous? For that matter, why not `COPY X -> Y`, since that's what it actually does?