Learning C++!
- GyroVorbis
- Elysian Shadows Developer
- Posts: 1874
- https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
- Joined: Mon Mar 22, 2004 4:55 pm
- Location: #%^&*!!!11one Super Sonic
- Has thanked: 81 times
- Been thanked: 64 times
- Contact:
Learning C++!
Man, am I happy. I just barely got paid. I had enough to get a VERY respectable C++ book. I have been really hauling. The lesson plans are supposed to take a day. I did like 4 yesterday and plan on doing even more today. C++ is VERY different from anything I've ever done. I've only been familiar with interpreted languages. My language of choice is Perl (most closely related to a shell like Bourne) and I've only really made CGI scripts for my website. Heh, I wanna try to make a build script with perl.
C++ and Perl syntax are VERY lord, VERY different.
Check this out!
(random hello world to compare both!)
PERL
#!/usr/bin/perl
print "Hello Dreamcast!\n";
C++
#include <iostream>
int main()
{
using std::cout;
cout << "Hello Dreamcast\n";
return 0;
}
COMPARE! I don't know about you, but since that one day of reading C++, I've been able to look at Dreamcast code which looked like jarled mess and say "Holy Crap, I see what they are doing there!". I can't like understand it all, but I'm getting there.
Dunno about you guys, but I look forward to working with da Dreamcast.
C++ and Perl syntax are VERY lord, VERY different.
Check this out!
(random hello world to compare both!)
PERL
#!/usr/bin/perl
print "Hello Dreamcast!\n";
C++
#include <iostream>
int main()
{
using std::cout;
cout << "Hello Dreamcast\n";
return 0;
}
COMPARE! I don't know about you, but since that one day of reading C++, I've been able to look at Dreamcast code which looked like jarled mess and say "Holy Crap, I see what they are doing there!". I can't like understand it all, but I'm getting there.
Dunno about you guys, but I look forward to working with da Dreamcast.
-
- Mental DCEmu
- Posts: 415
- Joined: Thu Oct 10, 2002 7:18 pm
- Has thanked: 0
- Been thanked: 0
- Contact:
you might want to get a VERY good C programming book as well.
Seeing that 98% of the programs are made in C not C++ (and yes they are different enough for the distinction)
See? There's a difference....
Seeing that 98% of the programs are made in C not C++ (and yes they are different enough for the distinction)
Code: Select all
PERL
#!/usr/bin/perl
print "Hello Dreamcast!\n";
C++
#include <iostream>
using namespace std;
int main()
{
cout << "Hello Dreamcast, in C++" << endl;
return 0;
}
C
#include <stdio.h>
int main ()
{
printf("Hello Dreamcast, in C\n");
return 0;
}
-
- DC Developer
- Posts: 80
- Joined: Wed Oct 17, 2001 7:44 pm
- Location: Sweden
- Has thanked: 0
- Been thanked: 1 time
just for the fun of it, hello world in sh4 asm:
Code: Select all
.global _main
_main:
sts.l pr,@-r15
mov.l printf,r0
mov.l stringaddr,r4
jsr @r0
nop
lds.l @r15+, pr
rts
nop
string:
.string "Hello Dreamcast in sh4 assembly\n"
stringaddr:
.long string
printf:
.long _printf
- toastman
- Iron Fist of Justice
- Posts: 4933
- Joined: Sat Nov 10, 2001 3:08 am
- Location: New Orleans
- Has thanked: 0
- Been thanked: 0
- Contact:
Ooh ooh ooh lemme try
Code: Select all
.global _main
_main:
#Store string long from pr to address of register 15
sts.l pr,@-r15
#Move long from register 0 to printf
mov.l printf,r0
#Move long from register 4 to stringaddr
mov.l stringaddr,r4
#Jump and set register using the address of register 0
jsr @r0
#No operation
nop
#Load string long from pr to the address at register 15
lds.l @r15+, pr
#Return
rts
#No operation
nop
string:
.string "Hello Dreamcast in sh4 assembly\n"
stringaddr:
.long string
printf:
.long _printf
No signature.
-
- DC Developer
- Posts: 80
- Joined: Wed Oct 17, 2001 7:44 pm
- Location: Sweden
- Has thanked: 0
- Been thanked: 1 time
toastman, store string/load string?? ![Very Happy :D](./images/smilies/icon_e_biggrin.gif)
Acctually, you got it all wrong except nop and rts... sorry...
(note: sts mean STore System register and lds means LoaD to System register. It's all in the SH-4 programming manual)
q_006,
You shouldn't wait for knowledge to come to you, you should seek it out for yourself.
That being said the sh4 programming manual is great: http://www.renesas.com/avs/resource/jap ... 56_sh4.pdf
sh-elf-gcc with the commandline option "-save-temps" is also great. That commandline option saves the temporary assembly output files so that you can watch the code it generates. Though, I must warn you that gcc does not generate all that good assembly code.
here is the code again, but this time with comments:
And it's as easy as that ![Smile :)](./images/smilies/icon_e_smile.gif)
Now it's time for me to get some sleep, nighty.
![Very Happy :D](./images/smilies/icon_e_biggrin.gif)
Acctually, you got it all wrong except nop and rts... sorry...
(note: sts mean STore System register and lds means LoaD to System register. It's all in the SH-4 programming manual)
q_006,
You shouldn't wait for knowledge to come to you, you should seek it out for yourself.
That being said the sh4 programming manual is great: http://www.renesas.com/avs/resource/jap ... 56_sh4.pdf
sh-elf-gcc with the commandline option "-save-temps" is also great. That commandline option saves the temporary assembly output files so that you can watch the code it generates. Though, I must warn you that gcc does not generate all that good assembly code.
here is the code again, but this time with comments:
Code: Select all
! First of all, lines beginning with a "!" are comment lines :)
! The link below is to the "SH-4 programming manual", that would be,
! your bible from now on.
! http://www.renesas.com/avs/resource/japan/eng/pdf/mpumcu/e602156_sh4.pdf
! This file was written by Fredrik Ehnbom June 1st, 2004 and probably
! contains some errors here and there in the comments :)
! Anyway time to start explaining this hello world example!
! ------------------------------------------------------------------------
! The line below tells the assembler that the function _main will be used from
! another file. So if you have a project with two files, and you
! want to be able to call this "_main"-function from another file,
! you need to declare it global first. The same thing goes if you want
! to access arrays or variables or whatever from another file.
! See it as some sort of "export" command.
.global _main
! All text-strings ending with a ":" are called labels. Labels are an
! easy way to mark an memory address in a program. They are used
! for looping, functions, variables etc. Basicly, when you know you'll
! need to be able to rearch a certain memory address in the program,
! mark it with a label.
! The underscore in the beginning is there since this method will be called
! from a c-file. Since c is one level higher than asm, it prepends an underscore.
! This is to avoid nameconflicts between assembly functions and c-functions.
! C++ is one step higher than c so it prepends an extra underscore (a total
! of two underscores per c++ function or variable that is :))
!
! So, a file named "void init()" in c would be "_init:" in assembly.
! The same goes for data values. "long init_data = 0;" in c becomes
! "
! _init_data:
! .long 0
! "
! in assembly
_main:
! r15 contains the stack. Since the pr-register will get scratched by
! the "jsr" down below, and we want to return properly in the end,
! we need to store the old value of pr on the stack. Oh, pr is
! the "procedure register" and it stores the return address.
sts.l pr,@-r15
! This moves the value in the label "printf" to r0.
! As you can see below, the "printf" label contains a long value
! for "_printf". That would be the label "printf" contains the address
! of the label (function) "_printf". Remember what I said above about
! labels with underscores?
mov.l printf,r0
! This instruction is basicly the same as the above. We have a label
! called "stringaddr" that contains the address to the label "string".
! When calling functions, the first non-float value is put in r4 and this
! is what we do here. For more information about the sh4 calling sequence,
! read here:
! http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcechp40/html/_callsh4_SH_4_Calling_Standard.asp
mov.l stringaddr,r4
! jsr means "jump to sub-routine" and that is exactly what
! this instruction does. First The pr-register gets the value "pc + 4".
! This is where the program will resume executing after the subroutine
! has returned. Each instruction is worth two bytes so pc+4 in this
! case would be the "lds.l"-instruction down below. After that has been saved to
! pr it takes the value in rx (in this case r0) and puts it into the
! program counter (pc) register, which basicly means that execution will
! continue at that value (memory address). In our case, that means that the c-function
! "void printf(...)" will be called.
jsr @r0
! All branching routines (except bt and bf) on the sh-4 are delayed branches.
! This means that before starting to execute at the memory address set in the pc-register
! by the branching instruction (in our case jsr above), the instruction right after the
! branching instruction is executed first (in our case the nop below).
! So the sequence would be:
! 1. jsr
! 2. nop
! 3. instruction at memory address (pc)
! To be honest, I'm not exactly sure why delayed branching is usefull. :)
! Maybe the execution of the instruction in the delayed slot is for free or something,
! I don't know..
! Anyway, nop means "no operation" and does nothing at all :)
nop
! Now this is the point to where the program will continue executing when
! "printf" returns.
! This instruction restores the value of pr as it was when this function
! was called (remember the save above).
lds.l @r15+, pr
! rts means "return from subroutine", and that is
! exactly what it does. It sets pc register to pr (pc = pr).
rts
! Remember above that all branching instructions (except bt and bf) are delayed branches.
! This applies to rts too, so we put a nop here again.
nop
! stores a string-value with the label "string"
string:
.string "Hello Dreamcast in sh4 assembly\n"
! .align aligns data at the specified boundary. I'm not sure if this is needed
! here, but just to be safe I added it. Read section 2.5 in the "SH-4 Programming
! manual" for more information about aligning.
.align 4
! stores a label called "stringaddr" containing the address to the label (variable) "string"
stringaddr:
.long string
! stores a label called "printf" containing the address to the label (function) "_printf"
printf:
.long _printf
![Smile :)](./images/smilies/icon_e_smile.gif)
Now it's time for me to get some sleep, nighty.
- GyroVorbis
- Elysian Shadows Developer
- Posts: 1874
- Joined: Mon Mar 22, 2004 4:55 pm
- Location: #%^&*!!!11one Super Sonic
- Has thanked: 81 times
- Been thanked: 64 times
- Contact:
OH MY GOD! I will never say C++ is complex. I should be ashamed!just for the fun of it, hello world in sh4 asm:
![Embarassed :oops:](./images/smilies/icon_redface.gif)
I know I shouldn't ask, but can anybody say hello world in .csh? I think it is called the "C shell". I know it should've gotten the knife a long time ago, but this is just for the fun of it.
Please people, feel free to contribute your "Hello Worlds!" to the list! (Maybe we can start like a "Hello World Encyclopedia".
![Very Happy :D](./images/smilies/icon_e_biggrin.gif)
- SinisterTengu
- DC Developer
- Posts: 382
- Joined: Wed Oct 17, 2001 7:44 pm
- Location: Arlington, WA
- Has thanked: 0
- Been thanked: 0
- GyroVorbis
- Elysian Shadows Developer
- Posts: 1874
- Joined: Mon Mar 22, 2004 4:55 pm
- Location: #%^&*!!!11one Super Sonic
- Has thanked: 81 times
- Been thanked: 64 times
- Contact:
-
- DC Developer
- Posts: 968
- Joined: Tue Feb 11, 2003 4:12 pm
- Location: In a Dream
- Has thanked: 5 times
- Been thanked: 6 times
quarn, toast's program was correct. I think he just dumped gcc's asm output and commented it (erroneously).
If I might venture the correct comments:
If I might venture the correct comments:
Code: Select all
! _main is a fuction visible from other files
.global _main
! begin main function
_main:
! save the stack address of the function that called _main
sts.l pr,@-r15
! save the address of 'printf' function in register r0
mov.l printf,r0
! save the address of our string in register r4 so that function printf can find it
mov.l stringaddr,r4
! call printf function
jsr @r0
nop
! printf has returned. restore the stack address to where it was
lds.l @r15+, pr
! go back to whoever called _main
rts
nop
! our string
string:
.string "Hello Dreamcast in sh4 assembly\n"
! the address of our string
stringaddr:
.long string
printf:
.long _printf
behold the mind
inspired by Dreamcast
inspired by Dreamcast
-
- DC Developer
- Posts: 80
- Joined: Wed Oct 17, 2001 7:44 pm
- Location: Sweden
- Has thanked: 0
- Been thanked: 1 time
I didn't complain about "toast's" program. I complained about his comments for my asm code I posted above.nymus wrote:quarn, toast's program was correct. I think he just dumped gcc's asm output and commented it (erroneously).
And your comments about sts.l and lds.l are wrong..
Besides, I allready wrote a huge explanation (maybe too huge :)) of the code.
-
- DC Developer
- Posts: 968
- Joined: Tue Feb 11, 2003 4:12 pm
- Location: In a Dream
- Has thanked: 5 times
- Been thanked: 6 times
Sorry. I must have read wrong. And to correct the lds instructions: The sts.l pr/lds.l pr saves/restores the return address.quarn wrote:I didn't complain about "toast's" program. I complained about his comments for my asm code I posted above.
And your comments about sts.l and lds.l are wrong..
Besides, I allready wrote a huge explanation (maybe too huge) of the code.
![Embarassed :oops:](./images/smilies/icon_redface.gif)
Thanks
![Wink ;)](./images/smilies/icon_e_wink.gif)
[edit]: Crap! I thought your post was a dump of a tutorial which is why I didn't read it
![Very Happy :D](./images/smilies/icon_e_biggrin.gif)
Unless I'm mistaken, delayed branches are a consequence of superscalar execution where the sh4 wants to execute 2 instructions in paralell. As such it must fetch 2 instructions, compare and if possible execute them in parallell. When you branch, the 2nd instruction occupies the 2nd slot that would have been used for comparison and must be filled somehow.
behold the mind
inspired by Dreamcast
inspired by Dreamcast