Assembly Language Pointer Issue

Stuck with a problem in your code? Seek help here.

Assembly Language Pointer Issue

Postby evansste » February 1st, 2016, 7:02 pm

This topic is related to one that I posted (and answered) earlier today. Though one problem was solved, another has arisen.

After assembling and linking the following program, I get an error message when I run the executable. Here's the program:
Code: Select all
.section .data
A:
   #.int 0xE7C6E069, 0x09AA3796
   .int 0xE7C6E069
.section .text
.globl _start
_start:
   nop
   movl $A, %edi   
   testl $0x00000080, 0(%edi)


When running this program I get the following error message:
Code: Select all
evansste@evansste-desktop:~$ ./test
Segmentation fault (core dumped)


I performed a search for this error online, and nearly everyone says that the problem is likely due to a pointer that points to a section of memory that can't be used. This sounds specific enough, however, this program is extremely simple. All it does is load a register with a declared value's address, and then it tests to see whether or not a specific bit has been set for the data at that location.

Because the error seems to be related to a bad pointer, I would guess that the problem has something to do with line 9. The book that I'm reading clearly shows that this is the way to load a pointer into a register, but clearly, something's not right. Does anyone have any ideas as to what may be going on?

I'm running Lubuntu 14.04.3 LTS. Here's more information pertaining to the package that I'm using that has the assembler:
Code: Select all
evansste@evansste-desktop:~$ dpkg -l | grep binutil
ii  binutils                                   2.24-5ubuntu14                          i386         GNU assembler, linker and binary utilities


If anyone has experience with assembly language, this is probably an easy one for you. Everything I've learned is coming from a single book that I'm reading, and according to the book, there should be no problem.

Any guidance is greatly appreciated.
evansste
 
Posts: 99
Joined: October 8th, 2014, 8:19 am

Re: Assembly Language Pointer Issue

Postby st2000 » February 2nd, 2016, 8:57 am

Maybe someone sees a specific reason for the seg-fault. But in general terms, if you are just starting out writing assembler, why not try on an embedded processor with no operating system? What you are (likely) up against is the OS using memory management to prevent you from unstabilizing your Linux computer. In an embedded processor writing bear-metal (no operating system) code you are in charge and can do what ever you want. Even crash.

The following is all guessing. But if you still want to write & run with in an OS and use variables, see if you can find an example where you can define some space in your code with labels and use the labels as arguments to your op codes. This way at least you know where relative to your code your variables are being stored.

---
Ah, well, more guessing, but if you don't put a return at the end of your assembler code ... well, where does the processor end up? Could it be that you're just executing what ever comes next in memory and eventually end up someplace where you shouldn't be causing a memory violation and a seg-fault?
st2000
 
Posts: 1454
Joined: February 3rd, 2011, 6:10 pm

Re: Assembly Language Pointer Issue

Postby evansste » February 2nd, 2016, 3:36 pm

Thanks, st2000.

Your last observation was right on the mark. I was only towards the beginning of the book that I'm reading, and I didn't realize that unless your program exits properly, you're bound to have issues.

I've gotten so used to writing programs that don't need to compile, that I didn't figure the necessity to exit the program. The book that I'm reading is an excellent book, but the author doesn't thoroughly handle system calls until chapter 12. After my post, I decided to scrutinize his example programs before finally realizing my problem. Because the program never exited, your last observation explains exactly what was happening.

It's funny that you should mention bare metal programming. That's exactly what I'm possibly working towards, and I just learned the term a little over a week ago.

The first computer I ever learned to program was an Apple IIE. If you're old enough to remember, they had no operating system. You wrote your programs to a floppy disk. If you wanted to run a different program, you would use a different disk -- That's how I remember it anyway. Computers don't generally operate that way anymore, and when I was searching for answers, I read questions that many people were asking. They wanted to know if it's possible to run a computer without an operating system. It's funny how we've come full circle.

Anyway, thanks again for your input. As always, your help is greatly appreciated.
evansste
 
Posts: 99
Joined: October 8th, 2014, 8:19 am

Re: Assembly Language Pointer Issue

Postby st2000 » February 3rd, 2016, 6:58 am

The first computer I ever learned to program was an Apple IIE. If you're old enough to remember, they had no operating system.


Yes it did. I don't know what Jobs and Wozniak called it. But it had an OS. Almost all computers then and now have a very basic OS on the mother board that knew how to go out and boot off storage devices and pull in a bigger "OS" program. Heck, a lot of computers have multiple layers of booting. The thought is in order to upgrade one of these layers of OSs you need something to boot before it. The theory is that the 1st layer is sooo fundamental that it couldn't possibly have been implemented incorrectly and would never have to be upgraded in the field. (Yeah, right.)

The Apple I, II, II* were 6502 based. One of the easier processors to learn machine language on. The commands fit into a "human" memorable matrix of op-codes. Unlike the 'x86 lineage where the joke was (and might still be) that all the new processors could still run 4004 code. (This is probably not true, but I'm betting there's a lot of legacy stuff in a modern Intel processor.)

Even the Arduino (if we stretch the definition of what an OS is) has resident "Arduino" code inside the Atmel processor that is (supposed to be) never erased. If you do manage to erase it, you have bricked your Arduino. Not to fear, there are plenty of howto's on the web which essentially use a 2nd processor to re-program the Aruduino code into the target processor.

As for bare metal programming - you know you've made the jump because you usually need (surprise) an extra processor (a pod or something similar) to program the target processor. Like a Microchip.com pickit3 pod. Sometimes is doesn't look like you need this pod. Like when you buy a ti.com LaunchPad. But if you look closely, you will notice that there are 2 processors on the LaunchPad board.
st2000
 
Posts: 1454
Joined: February 3rd, 2011, 6:10 pm

Re: Assembly Language Pointer Issue

Postby evansste » February 3rd, 2016, 8:06 am

What you're saying makes sense about the Apple IIE. I just remember being able to turn it on and type BASIC commands at the prompt. The same thing is true for the TRS-80.

I had (still have) a TRS-80. I bought it from a friend, but he didn't have a disk drive for it. When I turned it on, I could write a program on it. However, if I powered it down, I lost everything.

I also agree with you about learning machine language on the Apple processor. My introduction to assembly language was with a book that showed how to program a 68HC11 Microcomputer trainer. It was an excellent place to start since this current book would have been a lot more difficult to follow without having been exposed to the 68HC11.
evansste
 
Posts: 99
Joined: October 8th, 2014, 8:19 am

Re: Assembly Language Pointer Issue

Postby bandersnatch » February 6th, 2016, 5:38 pm

Hi,
Ah........ the 6800 & 6502 series.....sigh!!!.....life used to be so simple :^))

Just a quick suggestion on experimenting with assembly language.

Exploring assembly language coding is much easier if you use a higher-level language framework
This allows you to print progess messages, inermediate results and is generally less confusing/frustrating

The simplest method is to wrap your assembly language code in C:
E.g.
http://www.codeproject.com/Articles/159 ... bly-in-C-C

Once you are familiar with the process I reckon you will agree that this make things easier & allows
you to experiment more freely...

STFB
bandersnatch
 
Posts: 150
Joined: September 17th, 2014, 12:06 pm

Re: Assembly Language Pointer Issue

Postby evansste » February 6th, 2016, 8:16 pm

Thanks for the suggestions, bandersnatch.

Fortunately, my program isn't overly complicated, so I've been able to write everything in assembly. So far, so good.

I'm one of those people who never really mastered C or C++. When I was in college, I was in a class that required us to write programs in C++. I still have the book and everything. Unfortunately, once the class was over, I didn't use what I had learned, and forgot a lot of things. I then started learning it again, several years ago, with a different book. I wrote the simple programs that I needed, at the time, and then stopped using it again.

All of that's been years ago and I never really programmed with it long enough for me to completely master it. Once I started writing programs in MATLAB, I simply wrote everything with that, and only use C++ when I absolutely have to.

C and C++ would be excellent languages to master since nearly everything is written with them. Maybe I'll master it one of these days.

Anyway, thanks for the good advice. The book, that I'm reading, makes the same suggestion. You know it's good advice when more than one person suggests it.
evansste
 
Posts: 99
Joined: October 8th, 2014, 8:19 am

Re: Assembly Language Pointer Issue

Postby bandersnatch » February 7th, 2016, 3:58 am

Hi

evansste wrote: Once I started writing programs in MATLAB, I simply wrote everything with that, and only use C++ when I absolutely have to.

C and C++ would be excellent languages to master since nearly everything is written with them. Maybe I'll master it one of these days.


Hmm... ironically, programming in C is much closer to assembly language than you might think.
Some of the older forum readers may remember the comment "C is just a souped up macro assembler" and
there is a surpising amount of truth in this
I strongly suspect that many things in C will suddenly make sense to you once you have written enough assembly language code.

You really only need to know how to use main() , __asm__ ( ) & printf() for your purposes and this will still make things much easier.

if C seems like too much work then you can also call assembly code direct from Matlab (!)
http://de.mathworks.com/help/polyspace_ ... ibute.html
This may appeal to you more if you are comfortable with Matlab.

The legend is that hardcore geeks program only in the command shell using "ed" & "asm".
This is initially fun but becomes tedious for non-trivial programs.

On my first computer I had to write the assembly language code with a pencil,
manually convert the code into binary with a pencil & manually enter each line of code
using binary address & data switches on the front panel.
This was also initially fun (I was young & needed the money :^)) but quickly became tedious (!)

The main point is to speed up the "edit/compile/run" cycle so that you can focus on your assembly language coding.

Things have come a long way since then and IMHO you should soon bite the bullet & get to grips with using an IDE like Eclipse
for coding. Eclipse can be a pain in the a** to configure but will save you time in the long run once you get past the initial confusion.
Many things will be familiar to you from the Matlab environment & getting to grips with Eclipse will be valuable to you in the long run.

Good luck with your coding adventure!

STFB
bandersnatch
 
Posts: 150
Joined: September 17th, 2014, 12:06 pm

Re: Assembly Language Pointer Issue

Postby christspringer271 » May 21st, 2016, 2:27 am

Wow! I have been using Assembly language way back 4 years ago! And I don't have any intention of using it back again. :roll:
User avatar
christspringer271
 
Posts: 4
Joined: May 19th, 2016, 1:24 pm
Location: 2879 Christie Way Quincy, MA 02169

Re: Assembly Language Pointer Issue

Postby TravisCucore » November 7th, 2016, 5:55 pm

Because the error seems to be related to a bad pointer, I would guess that the problem has something to do with line 9. The book that I'm reading clearly shows that this is the way to load a pointer into a register, but clearly, something's not right. Does anyone have any ideas as to what may be going on?"


I think you're probably right about that. I've not worked with whatever chip you are working on. but I can make a couple of suggestions. First, make sure that you're data will fit where it is going without degradation(looks like you thought about that as you used a long). Also, make sure 0xA is a memory location you can access. If you are not trying to access that location (meaning you want to load that value into %edi) try adding the "immediately" prefix (if such a thing exists for your chip). I'm used to using # for that so it would look like this (#$0xA).

I looked at some material found on google, and it looks like (I may have bad information) for your syntax, 0xA is a label (the basis for pointers in C) Basically, it is pointing to memory location 0xA. That is a very low location, and if your chip is memory mapped (meaning your pins are tied to lower memory locations) you may need to use an intermediary register to store the value of contained at location 0xA).

If none of that helps, I'd check the next line. To check a single bit, I would create a bit-mask, use a logical & with the data to check if your bit was set, then check the CCR (condition code register "zero flag") If the result is zero the bit isn't set, if it is one, it is set.

I hope any of this was helpful, I'm not an assembly ninja, I just like to play with it. I agree with most of the other comments. A less frustrating, faster development approach would be to write in C, and slip ASM into your code as needed.
TravisCucore
 
Posts: 46
Joined: May 14th, 2013, 1:02 am


Return to Help Me! Software

Who is online

Users browsing this forum: No registered users and 2 guests