LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 03-19-2011, 02:14 AM   #1
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Rep: Reputation: 15
Question 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
 
Old 03-19-2011, 10:10 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 03-19-2011, 11:04 AM   #3
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by theNbomr View Post
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 ?
 
Old 03-19-2011, 11:39 AM   #4
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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.
 
Old 03-19-2011, 11:50 AM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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.
 
Old 03-21-2011, 07:57 AM   #6
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Original Poster
Rep: Reputation: 15
oh, thanks you guys,that is helped,i am new to assembly language,thanks very much for your reply!have a good day!
 
Old 03-21-2011, 02:35 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is Assembly Language considered a Structured Language? theKbStockpiler Programming 4 01-30-2011 09:09 AM
Assembly Language Books JMJ_coder Programming 3 05-06-2008 04:04 PM
assembly language in linux herbertgnanaraja Programming 12 09-21-2006 03:52 AM
assembly language directives ashlesha Programming 2 07-03-2006 09:20 PM
Assembly Language syntax ashlesha Linux - Newbie 4 06-30-2006 04:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration