Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   I Have A Question About Pointers (http://www.chiefdelphi.com/forums/showthread.php?t=93687)

Ether 05-04-2011 22:21

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by demosthenes2k8 (Post 1049948)
the compiler can probably write better assembly than a human.

Although I agree that it is seldom necessary (or desirable) to write assembly code, an experienced assembly programmer can in many cases implement optimizations that a compiler won't use. The downside is that the code can be a brittle maintenance nightmare.



mikets 06-04-2011 01:24

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by davidthefat (Post 1050056)
According to Intel there is http://www.intel.com/products/processor/manuals/
And I read that Intanium was the one that was NOT compatible with x86, that is why Intel uses AMD's x86-64 architecture.

Well, I guess I did not follow the Intel terminology as close as I thought. In any case, Intel has three architectures: Itanium 64 (IA64) is not compatible with anything, x64 which is compatible with amd64 and the old 32-bit whatever it is called (x86).

davidthefat 06-04-2011 01:58

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by mikets (Post 1049986)
In Intel processor architecture, there is the concept of real mode versus protected mode. But there is no such thing in PPC. In PPC, you probably mean kernel mode versus user mode. But that's entirely different thing. For historic reason, Intel processor boots up into real mode where it uses real mode address space seg : offset to form a 20-bit physical address. That will allow you to access only the low 1 Mbyte of memory. PC BIOS runs in this environment and loads the OS loader into memory (Actually much more complicated than that, but this is the simplified view). The OS loader will then set things up and switch the CPU to go into protected mode. The setup includes setting up the virtual memory address tables GDT, LDT and the interrupt table IDT. So once the OS is booted, the CPU is already in protected mode. Everything including both the OS kernel and applications are run in protected mode. However, in protected mode, there are different privileges. Again, for historic reason, Intel processor supports 4 different privileges (rings). But most of the operating systems support only 2 privileges and they are usuually called kernel mode or superviser mode and user mode. Applications are run in user mode and OS kernel, device drivers are usually run in superviser mode.
I am not very familiar with vxWorks but I believe your program is actually compiled as a downloadable kernel module. So I assume it must be running in kernel mode as well which means your program can misbehave and kill the entire system if you are not careful (e.g. run away pointers). BTW, you can do pointer arithmetics in C/C++. There is no need to do that in assembly. But if you are not careful, you can trash memory. For vxWork, you can probably trash kernel mode memory as well and cause a system crash. For Windows application, for example, a run away pointer can only trash application memory. Kernel mode memory is protected from applications. So your application may crash but the OS will be fine. You can just kill the app and move on. However, for Windows kernel mode drivers, they have privilege to access kernel mode memroy. If they have a run away pointer, they could trash kernel mode memory and bring the system down (Blue-Screen aka BugCheck).

Actually it might be in kernel mode. That explains why vXWorks does not have a system call system. You just go do it for yourself, there is no asking for the OS to do it for you.

Techhexium 06-04-2011 03:11

Re: I Have A Question About Pointers
 
Itanium or IA-64 is in a way pure 64-bit architecture. Because there is little to no compatibility with other architectures, Microsoft is finally abandoning it so there is no more future IA-64 Windows. x64 is actually an extension of x86, I think that's why some call it x86-64.

Greg McKaskle 06-04-2011 08:34

Re: I Have A Question About Pointers
 
Learning assembly and low-level computer architecture are good things. Putting your experiments on your teams robots -- maybe not.

Assembly programming is a very unforgiving task, and is best done with a net. My CS degree was a few years back, but they still required an assembly course. It was done completely virtual, on a mainframe, because your program would crash about 90% of the time. Since it was virtual, the "processor" was frozen so that you could examine the remains and determine where you went wrong, modify code, and run the experiment again. Even with the help of good tools such as this, it was a painful reminder of just how low-level and complex computers are.

The good thing about that experience is that from time to time, I need to look under the covers and try to understand a bug, an optimization, etc. and being able to grok the assembly and low-level parts of the computer is useful. The rest of the time it would only slow me down by about 100x.

Once you have a good PPC assembly reference and understand the basics of the architecture, I'm pretty sure that you can run your C++ code in vxWorks, hit a breakpoint, show the registers, the stack, and the disassembly. Even better, some environments show you the mixed view of your code -- a line of C and the assembly for that line. This will allow you to see how the compiler produces assembly, learn its tricks, and do it much faster than starting from scratch. It is informative to look at array indexing code, function calls, loops, and other high-level structures that make you productive. It is also informative to intentionally put some common bugs into your code and step through them in assembly to better understand what they cause the computer to do.

Greg McKaskle

mikets 06-04-2011 12:38

Re: I Have A Question About Pointers
 
Are you trying to learn an assembly language or it must be PPC assembly? If learning x86 assembly is good enough, you can use your existing tools. Since your first post seems to be C++/C#, you probaby have visual studio. If so it supports the syntax of _asm {<some assembly code>} mixed in with C code. So you can progressively add more assembly code to your project. Another useful thing to do is to configure your compiler to generate assembly listing so that you can see how each C line is converted into assembly.

Ether 06-04-2011 12:45

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by mikets (Post 1050311)
Another useful thing to do is to configure your compiler to generate assembly listing so that you can see how each C line is converted into assembly.

Yes. And you may be appalled at what you find.



demosthenes2k8 07-04-2011 22:21

Re: I Have A Question About Pointers
 
I'm pretty sure you add a -S to the command line options to do that...I'm gonna try that out now.

After trying that out, HUGE MISTAKE. I used to think my code was elegant...

davidthefat 07-04-2011 22:29

Re: I Have A Question About Pointers
 
Yes, I used the disassembly tool in the debugger, I found that a lot of the code was filled with nop (no operation) but why? Is it to compensate for the data transfer from the ram to the cache? That seems unlikely

mikets 07-04-2011 22:34

Re: I Have A Question About Pointers
 
Most likely for alignment. Usually in between functions.

wireties 07-04-2011 23:24

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by davidthefat (Post 1050191)
Actually it might be in kernel mode. That explains why vXWorks does not have a system call system. You just go do it for yourself, there is no asking for the OS to do it for you.

The 6.X version of VxWorks does have a system call interface (and normal block, character and network drivers) and processes (which they call RTPs or real-time processes) but the robot code does not use them. But RTP support is not included in the cRio BSP (board support package) provided to FIRST. All the robot code runs in the kernel context or mode.

HTH

Ether 08-04-2011 08:53

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by wireties (Post 1050649)
...the robot code does not use them ... RTP support is not included in the cRio BSP (board support package) provided to FIRST. All the robot code runs in the kernel context or mode.

Keith, I'm not disagreeing with you, but how do you know all this? Is there a document somewhere which discusses this ?



wireties 11-04-2011 15:14

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by Ether (Post 1050704)
Keith, I'm not disagreeing with you, but how do you know all this? Is there a document somewhere which discusses this ?

NP Ether - I taught engineers how to use VxWorks (and embedded Linux) for 15+ years. Check Help->Help Contents->Wind River Documentation from the Workbench IDE. Under Guides->Operating System there is a Application Programmers Guide (which is about programming in a RTP) and a Kernel Programmers Guide (about programming in kernel space/mode) and a BSP Developers Guide (which is about building a board support package - which NI gives us). In our case, NI and/or WPI chose to not use RTPs and stick everything in kernel space/mode (probably because that is how LabView's code generator works). The C/C++ Developer User Guides and Getting Started->Tutorials are also most helpful.

HTH

Greg McKaskle 12-04-2011 06:32

Re: I Have A Question About Pointers
 
I'm not on the RT team, but the reason I was given was that when they did the board support, the RTP introduced too much overhead for the I/O drivers. Until FRC, the only C programmers for the cRIO were the NI developers, so they kept it in kernel mode. At that time, all other users of the cRIO used LV.

Exposing the C/C++ interfaces and tools, going to RTP was evaluated, but there would be a lot of work and testing involved, and it would mess with the I/O rates of various drivers.

Greg McKaskle

Ether 12-04-2011 09:23

Re: I Have A Question About Pointers
 
Quote:

Originally Posted by wireties (Post 1051762)
Check Help->Help Contents->Wind River Documentation from the Workbench IDE. Under Guides->Operating System there is a Application Programmers Guide (which is about programming in a RTP) and a Kernel Programmers Guide (about programming in kernel space/mode) and a BSP Developers Guide (which is about building a board support package - which NI gives us). In our case, NI and/or WPI chose to not use RTPs and stick everything in kernel space/mode (probably because that is how LabView's code generator works).

Are these three Guides freely available online somewhere, for those who do not have the Workbench installed ?




All times are GMT -5. The time now is 12:30.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi