Accepting your Limitations / Things I want to learn

General discussion for topics related to the FreeBASIC project or its community.
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Accepting your Limitations / Things I want to learn

Post by rolliebollocks »

I have severe gaps in knowledge as far as programming goes. As a kid I programmed in QB and Turbo C++ and then took G++ at the college level. The gap in knowledge is that I have no experience with low level programming. I suspect it's not as esoteric as it looks. Can anyone suggest a good resource for learning ASM?

I also want to learn more about how pointers work. Some of the stuff you guys can do with casting looks like voodoo to me. I'm missing something.

Also. Unions and Fields and reading and writing binary data. Clueless. These are the areas I need to expand into in order to push my limits

One more thing.

How can I make FB spit ASM that I can repaste into a BAS?
Jim Barlow
Posts: 43
Joined: Sep 23, 2005 0:37

Post by Jim Barlow »

Though I also know next to nothing about "ASM", it seems there are several different versions with different syntax floating around out there: MASM32, NASM, FASM and the GNU assembler. There are communities and forums related to each. Some even have RAD IDEs for making MS-Windows programs.

All that I have mentioned are free. If you want to learn 16 bit, there is Emu8086, which is an emulator with tutorials.

It seems to me that "ASM' isn't so daunting as learning all the details of the CPU. I know the (freely available) Intel data sheets are intimidating.

I think you just need to get some experienced advice on which path to take and make a beginning.

Jim
marcov
Posts: 3503
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Accepting your Limitations / Things I want to learn

Post by marcov »

rolliebollocks wrote:I have severe gaps in knowledge as far as programming goes. As a kid I programmed in QB and Turbo C++ and then took G++ at the college level. The gap in knowledge is that I have no experience with low level programming. I suspect it's not as esoteric as it looks.
No of course not. It is so dumb a piece of silicon can execute it :-)
Can anyone suggest a good resource for learning ASM?
There are many, but too many focus on relatively standalone ASM. I would try to wrestle yourself through some starter course, and then start to disassemble pieces of compiled code, and see if you can follow that.

Print out fragments, and keep track of the stack if you need to. It helped me.
I also want to learn more about how pointers work. Some of the stuff you guys can do with casting looks like voodoo to me. I'm missing something.
I would do that way, way before assembler. The exercise that really made me see the light was making a double linked list. E.g.

Make a simple phone directory, where a you have a struct TELEPHONEENTRY with

- name
- telephone number
- pointer to the next TELEPHONEENTRY or NUL if last.
- pointer to the previous TELEPHONEENTRY or NUL if first.

Have a list of _unsorted_ entiries

name - telephone number

in a file.

Now, load the file, and insert the items into the list maintaining the condition that all records are properly interlinked (next, previous) and that the first record has previous=nul and the last next=nul.

(look up "double linked list" on Google to find some more graphic explanations of the problem).

After properly building the list, open a second list with the same format as the first, which contains some random items to be deleted. Remove these items from the list, keeping the list intact. Make sure to test removing the first and last item.
Also. Unions and Fields and reading and writing binary data. Clueless. These are the areas I need to expand into in order to push my limits
One thing at a time. I think the double list exercise is a good one. Don't look the solution up (this is not school, you are doing this for your benefit, not mine), don't try to solve the problem a different way and avoid the hard parts. Be critical, and try to punch holes in your solution.

Post the solution here if you have found it, to have it checked for border conditions.

This is typically a first year exercise from a CS bachelor education, but one that is after the initial courses. (but that could be already after 10 weeks)
How can I make FB spit ASM that I can repaste into a BAS?
Forget about that. While possible in some cases, such things are never possible without understanding that assembler. Moreover, just copy-pasting would be besides the point, since it would not improve anything.
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Post by rolliebollocks »

Oh crap. When is this due by?

;)

I do know some LISP (forgot to mention) so I understand conceptually how lists work.

Gimme at least a couple days so I can make something that I can use anywhere. This will be a good exercise, I'm overly dependent on linear arrays.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

Compile with -r to make FBC save the ASM file.

You can learn the internal names/makeups for functions among other things. You'll notice every argument is first PUSH'd to the stack, then POP'd by the function that's called.

(You'll have to crawl around FB's source code for those procedures.)

Example: http://www.freebasic.net/forum/viewtopic.php?t=15710
I used -r and FBC's source code to create that.

(Occasionally there's a bug in how FBC and GfxLib2 are documented, which cost me hours of time before -_-...)
marcov
Posts: 3503
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Post by marcov »

rolliebollocks wrote:Oh crap. When is this due by?
I had a week, but that was when I was a full time student, and I don't know your circumstances.

So I'll be merciful (since i had other classes too), and grant you two.

I do know some LISP (forgot to mention) so I understand conceptually how lists work.
That is not important. The question is not if you understand the theory, but if you can make it work.
Gimme at least a couple days so I can make something that I can use anywhere. This will be a good exercise, I'm overly dependent on linear arrays.
Do not embellish, do not overengineer, stick to the assignment.
BasicCoder2
Posts: 3955
Joined: Jan 01, 2009 7:03
Location: Australia

Post by BasicCoder2 »

> I have severe gaps in knowledge as far as programming goes.
> As a kid I programmed in QB and Turbo C++ and then took G++
> at the college level. The gap in knowledge is that I have
> no experience with low level programming. I suspect it's
> not as esoteric as it looks. Can anyone suggest a good
> resource for learning ASM?
The great thing about FreeBasic is it does have an inline
assembler. I noticed some examples of how to use it in
the forum. You might start with something simple like
adding two numbers and move up to say sorting a list of
numbers and so on ...

JohnC
Last edited by BasicCoder2 on Sep 13, 2010 19:45, edited 1 time in total.
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Post by Richard »

rolliebollocks wrote:Some of the stuff you guys can do with casting looks like voodoo to me. I'm missing something.
Cast is just an instruction to tell the compiler you have changed your mind about the type of data your pointer is aimed at. Since arithmetic on a pointer is done in multiples of the size of the data's type, it enables the compiler generate the address arithmetic correctly and to give useful error messages. As an example, a pointer to a 32 bit Uinteger with RGB colour data can be cast to a Ubyte pointer which then gives access to the individual bytes within the Uinteger. Cast is not executed at run time. It only influences the pointer arithmetic code generated by the compiler.
relsoft
Posts: 1767
Joined: May 27, 2005 10:34
Location: Philippines
Contact:

Post by relsoft »

rolliebollocks wrote:Oh crap. When is this due by?

;)

I do know some LISP (forgot to mention) so I understand conceptually how lists work.

Gimme at least a couple days so I can make something that I can use anywhere. This will be a good exercise, I'm overly dependent on linear arrays.


What's wrong with linear arrays? They are the "IN" programming pattern these days when you talk about Data Oriented Design and Component Based Models.


ASM is not hard per se, it also follows logic. I've made a few ASM stuff myself but I really don't want to code in ASM ever gain. ASM code takes time to make, debug and modern compilers are very good optimizers. For me, ASM's speed could only be abused in tight loops.

Slow code would be slow code even in ASM. Case in point...

http://rel.betterwebber.com/junk.php?id=53


That's a 256 byte (yes 256 byte) radial tunnel demo I made in FASM a while back. Because of the 256 byte limitation, it's bound to be slow.

So don't worry if you don't know ASM, it's not going to be as useful as learning algorithms.

As for learning, I find it really easy to learn stuff if I print the article on paper(Though I convert the to PDF these days and read via my daughter's PSP). I find it hard to understand things when I read them in front of a monitor.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

Copy + Paste + Minor edits are perfectly acceptable. I'll say it again, premature optimization is the root of all evil. But I won't lie - I forget this often.

The first solution I come up with is often generic because I can by now perceive programming patterns. However, in practice, what I actually need is often slightly beyond the constraints of the pattern I perceived.

This is equally as time consuming as any mistake in programming :)

So I'm a fan of clear, direct, specific solutions.
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Post by rolliebollocks »

@anonymous1337

I completely agree. My first pass on anything is just about as generic as can be. I ask myself what the most basic manifestation of something is, then I make it, debug it, make it prettier, and then optimize it. And it takes me ages to get to the point where I feel comfortable optimizing it. I really want it

@Marcov

I have begun. So far I have in my type a linear bit array (a compound pointer via Callocate) and you can iterate either sequentially through the array (which is what not a linked list) or via previous->next which is a linked list.

I have an Add command, that's it.

Is it wrong to have the compound pointer as part of the List type? This seems like an unneccessary expenditure of memory.
marcov
Posts: 3503
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Post by marcov »

rolliebollocks wrote: I have begun. So far I have in my type a linear bit array (a compound pointer via Callocate) and you can iterate either sequentially through the array (which is what not a linked list) or via previous->next which is a linked list.

I have an Add command, that's it.

Is it wrong to have the compound pointer as part of the List type? This seems like an unneccessary expenditure of memory.
Stick to the definition. Each element is a struct with only the payload (some integer or a string, but something you can order) and next/previous which are pointers to elements . Each element is separately allocated via (C)Allocate and deleted items need to be deallocated properly.

This is an assignment, and is about algorithmatic correctness, efficiency is irrelevant. The whole idea is to practice keeping control of border conditions, and to do it often enough so that it becomes a second nature. Which gives you a bit a "feel" for pointers.

You need maintain two global variables, root, which is a pointer to the first element, and tail which is a pointer to the last element of the list.

When I define cur as a pointer to element, and say cur=tail; and then while (cur<>nul) {cur=cur.previous}, I'll ultimately arrive at root, and then at NUL

Similarly, if I say cur=root and do while (cur<>nul) {cur=cur.next}; I will walk all nodes in the list, finally arriving at tail, and then nul.

To avoid too much work in vain, you can post snapshots of what you have from time to time. Don't abuse that however, and don't get help too quick. It is the trying yourself that teaches stuff, not reading the solution of others.

It is like watching a soccer match. You get the theory but not the condition :-)
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Post by Richard »

rolliebollocks wrote:Can anyone suggest a good resource for learning ASM?
Learning Assembly language for the Intel x86, IA–32 = Intel Architecture – 32 bit.
If you can avoid inline assembly then continue to avoid it. The speed improvement may be only around about a factor of two. Inline assembly may make portability a problem later. It will limit automatic optimisation of C emitter code. Inline assembly will also be a problem if / when FB is ported to a new processor. If you have ever written C successfully then you may well survive x86 assembler. Still interested? then read on.

At present FB only uses the 32 bit instruction set so you can avoid the 64 bit extensions for now. Instructions from all earlier x86 processors are supported so legacy code will run on today's processors. Getting started is a problem because there is really no master reference for the hybrid format used. For example FB uses the ' instead of a ; as the comment symbol.

First you may as well download copies of these manuals for the Intel IA–32.
Vol 1. Basic Architecture.
Vol 2A. Instruction Set A to M.
Vol 2B. Instruction Set N to Z.
They can be found at http://www.intel.com/products/processor ... /index.htm

Next compile this small FB program with the compiler -r switch set.

Code: Select all

Asm
    nop
End Asm
Dim As Integer i
Asm
    nop
    nop
End Asm
i = i + 1
Asm
    nop
End Asm
It will generate a filename.asm file that contains something like this near the middle.

Code: Select all

nop
mov dword ptr [ebp-4], 0
nop
nop
inc dword ptr [ebp-4]
nop 
When you find an instruction you do not understand, look it up in the Intel manuals. Try other code to see how FB handles it.

Now broaden your reading to texts such as those here; http://webster.cs.ucr.edu/ Be aware that different x86 assembler formats exist and that you will need to become familiar with the differences so you can learn from all available sources. The mnemonics are usually the same but register names and addressing formats differ. Notice that the format employed by FB requires an explicit “ptr”.

Register names have been extended as registers have become wider. As an example the lower half of the “Extended” 32 bit accumulator EAX, is the 16 bit accumulator AX, which can be referred to as single byte registers, AH and AL. This gives the capability to run legacy code. Take a look at Intel IA–32 Vol 1. pages 3-14 onwards.

Early processors used 16 bit segment registers offset by 4 bits to generate a magnificent 20 bit address, a whole megabyte! They are now just a distraction. Avoid old segment code by using the 32 bit flat addressing mode as used by FB.

See Intel IA–32 Vol 1, page 3-4 for a general map of registers available in today's processors.
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Post by rolliebollocks »

@Marcov

Ok. I'll get rid of the bit array. I've been making a mess with them anyway.

Apparently, some of my UDTs are resistant to callocate, reallocate.

Has anyone run into that problem before?

@Richard

Ugh. This is the same reason I stopped taking Akkadian cuneiform.
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Post by Richard »

@ rolliebollocks. Way back in the early 1980's I was writing assembly code and getting on quite well cracking crypto systems when I found an interesting diversion. It was Persian Cuneiform. I worked away until I could read it phonetically. It all sounded like Ancient Greek to me, which was a good sign. Then late one night, after working away at Cuneiform, I was walking home by starlight, across a flat lawn. I heard a shout, then a dog bark as I awoke lying on my back to see the stars spinning above. I managed to crawl home and curled up on the door mat until I had recovered sufficient balance to reach the door handle. Next day the doctor tells me that I have a virus that upset my balance. Since that night I have been unable to remember anything I ever knew about Cuneiform. I just have a file of papers to show it was not all a dream. I got back to the more rational assembly code without missing a beat, and wonder to this day if the Cuneiform I learned is still in my brain somewhere. Did I loose only the fresh links to it and if so, can I ever regenerate them. I also wonder what else I might have forgotten that night but can't remember.
Post Reply