From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fc89c,97188312486d4578 X-Google-Attributes: gidfc89c,public X-Google-Thread: 1014db,6154de2e240de72a X-Google-Attributes: gid1014db,public X-Google-Thread: 109fba,baaf5f793d03d420 X-Google-Attributes: gid109fba,public X-Google-Thread: 103376,97188312486d4578 X-Google-Attributes: gid103376,public X-Google-Thread: 10db24,4cf070091283b555 X-Google-Attributes: gid10db24,public From: bokr@accessone.com (Bengt Richter) Subject: Re: What's the best language to learn? [was Re: Should I learn C or Pascal?] Date: 1996/08/23 Message-ID: <4vkjdn$8rc@news.accessone.com>#1/1 X-Deja-AN: 175979084 references: <4v26j4$hkg@news.csus.edu> <01bb8d2f$351b9500$32ee6fce@timhome2> <4vdon9$8nt@news1.mnsinc.com> organization: - newsgroups: comp.edu,comp.lang.ada,comp.lang.c,comp.lang.c++,comp.unix.programmer Date: 1996-08-23T00:00:00+00:00 List-Id: mdw@excessus.demon.co.uk (Mark Wooding) wrote: >Szu-Wen Huang wrote: >> Lofty, ungraspable concepts like: >> >> void swap(int *a, int *b) >> { >> int c = *a; >> >> *b = *a; >> *a = c; >> } >> ... >> swap(&a, &b); >> ... >> >> ? swap(), by the way, is a primitive for just about every algorithms >> text I've ever read. Does knowing the computer must: >> >> LOAD r1, a >> ADD something_else_totally_unrelated >> LOAD r2, b >> INC some_counter_from_another_place >> STOR r1, b >> INC that_same_counter_but_we_just_need_to_fill_the_slot >> STOR r2, a >> >> in order to swap two integers aid in the understanding of swap()? >> I agree that we need to break an algorithm down to primitives, but >> are you actually saying swap(), for instance, isn't primitive enough? >I think you're deliberately trying to choose a nasty architecture. >We're talking about teaching, I think, so why not choose an easy one? >; --- swap --- >; >; Just like void swap(int *a,int *b) >swap LDR a3,[a1] > LDR a4,[a2] > STR a3,[a2] > STR a4,[a1] > MOV pc,lr >Simple. (And now there's a 200MHz version of this processor, so it's >not /that/ old-fashioned.) >-- >[mdw] >`When our backs are against the wall, we shall turn and fight.' > -- John Major ^^^^^^^^^^^^^^^^^^^^^^^^--I chuckle every time. Is that a real quote? Here's what BCC32 -v -O2 shows via TD32 on a Pentium: ----------------------------------------------------- Turbo Debugger Log CPU Pentium swap: void _fastcall swap(int *a, int *b){ :0040107C 53 push ebx #tswap#7: temp = *a; :0040107D 8B08 mov ecx,[eax] #tswap#8: *a = *b; :0040107F 8B1A mov ebx,[edx] :00401081 8918 mov [eax],ebx #tswap#9: *b = temp; :00401083 890A mov [edx],ecx #tswap#10: } :00401085 5B pop ebx :00401086 C3 ret :00401087 90 nop ----------------------------------------------------- Or, taking advantage of 386+ architecture by assembler: Turbo Debugger Log CPU Pentium #swapx#11: mov ecx,[eax] :00401150 8B08 mov ecx,[eax] #swapx#12: xchg ecx,[edx] :00401152 870A xchg [edx],ecx #swapx#13: mov [eax],ecx :00401154 8908 mov [eax],ecx #swapx#14: ret :00401156 C3 ret ----------------------------------------------------- Either way, there are additional architecture and compiler/ assembler-specific things you have to understand beyond the C code representation, which in turn has things extraneous to the essential algorithm in the abstract. I view the imperfect match of expression language and idea as a necessary evil you have to put up with to communicate. Ideally the language will be appropriate and adequate, and both parties will be fluent. But some algorithmic poetry will probably not survive translation to assembler, so a language may have to be learned -- nay, mastered -- before the original can truly be appreciated. Assembler is only one language, with numerous dialects. But whatever language one uses to express something, it can be said of the expression, "Ceci n'est-pas une pipe." Regards, Bengt Richter I wish I knew Latin. I could pontificate so much more elegantly ;-)