Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
03-19-2011, 02:14 AM
|
#1
|
Member
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128
Rep:
|
assembly language! please help me! thanks in advance!
firstly,
could someone tell me what is current location counter? a dollar sign($). what does counter mean? does it means it tracks the address of at which your current program? for example
Code:
list BYTE 10,20,30,40
listsize = ($-list)
does this means if the address of list starts at 1000h,and then 1001,1002,1003 for every byte,and listsize = (1003h - 1000h),so listsize gets the result of 3h?
another question is
what if a string definition ending without a 0,a null byte? for example:
Code:
greeting BYTE "thanks for helping me with my question",(a 0 missing)
thanks very much
|
|
|
03-19-2011, 10:10 AM
|
#2
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
|
To understand this, you need to think like an assembler. As the code is being translated, the address at which each instruction and operand will be stored must get calculated. This is a sort of linear process, and as that process occurs, the next address at which the assembler will store an instruction or operand byte is recalculated. That address is the location counter. In the assembler language, you are given access to the location counter through the '$' macro.
In your example, it allows for a convenient way of calculating the size (number of addresses used) of a data area.
--- rod.
Last edited by theNbomr; 03-19-2011 at 10:11 AM.
|
|
|
03-19-2011, 11:04 AM
|
#3
|
Member
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128
Original Poster
Rep:
|
Quote:
Originally Posted by theNbomr
To understand this, you need to think like an assembler. As the code is being translated, the address at which each instruction and operand will be stored must get calculated. This is a sort of linear process, and as that process occurs, the next address at which the assembler will store an instruction or operand byte is recalculated. That address is the location counter. In the assembler language, you are given access to the location counter through the '$' macro.
In your example, it allows for a convenient way of calculating the size (number of addresses used) of a data area.
--- rod.
|
thanks,it seems that i need to take a good understand of it.
if i wanna see the result of this instruction,what can i do ?
|
|
|
03-19-2011, 11:39 AM
|
#4
|
Senior Member
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
|
I don't know of any way you can step through the assembly, nor can I remember how I got it to output stuff after the processor is in protected mode (ie. when it's not booting up). However, getting a good understanding of it, like you mentioned in your post, is easy
You just need to look at the code you posted. Simplisticly, It will generate this in memory:
Code:
Label Address Value
list 0000000 10
0000001 20
0000002 30
0000003 40
listsize 0000004 4
The 'list' at the start of your code is labelling the address where you want to store the 10, 20, 30 and 40. When you get to the "listsize = ($-list)", then as theNBomr said '$' is converted to the current address. In this case, it will be 0000004. It then subtracts the address 'list', which in this case is 0000000. The difference between them, 0000004-0000000 = 4 is the size in bytes of the list. Hopefully you can see that this will still work even if 'list' happens to be at a non-zero address
The reason you would use it is obvious if you consider the following alternative code:
Code:
list BYTE 10,20,30,40
listsize = 4
If you then wanted to add another list item, you would also have to remember to change the listsize variable. Even worse, you might be storing an enormous list of numbers, and then decide you want to delete a load of them - unless you know exactly how many you deleted, you have to go count all of the list items again! So the '($-list)' just saves you having to enter this information manually...
Hope this helps,
Last edited by Snark1994; 03-19-2011 at 11:40 AM.
|
|
|
03-19-2011, 11:50 AM
|
#5
|
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Rep: 
|
Hi -
Let me elaborate on what's being said:
1. An "assembler" is a program that converts source code into binary machine code.
You write the source code with a text editor; you execute the binary machine code on your PC.
2. This source:
Code:
list BYTE 10,20,30,40
listsize = ($-list)
will conceptually generate something like this binary:
Code:
Label Address Value
list 0000000 10
0000001 20
0000002 30
0000003 40
listsize 0000004 4
3. The point is that "($-list)" is an ASSEMBLY-TIME thing. The number "4" is generated long before your PC is ever handed a binary to execute; the number is created and embedded in the object file itself.
In other words, the "location counter" we're talking about is for the ASSEMBLER. It has nothing to do with what's being executed when you actually run the program.
PS:
If you want to examine an object file, you can use "objdump".
Last edited by paulsm4; 03-19-2011 at 11:52 AM.
|
|
|
03-21-2011, 07:57 AM
|
#6
|
Member
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128
Original Poster
Rep:
|
oh, thanks you guys,that is helped,i am new to assembly language,thanks very much for your reply!have a good day!
|
|
|
03-21-2011, 02:35 PM
|
#7
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
|
Glad to hear you have been helped.
Please note that your subject line does not require phrases like 'please help me!', as it is implied that by posting you are asking for help (this is Linux Questions, after all). It would be helpful to everyone if you would use wording that better declares the subject matter. A big part of the value of LQ is that the material is archived and searchable. Therefore, when more descriptive language is used, the chance of someone finding it in a search becomes better.
--- rod.
|
|
|
All times are GMT -5. The time now is 03:45 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|