LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 06-13-2011, 07:16 AM   #1
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Rep: Reputation: 15
hey guys! assembly language and i need your help! :)


i am reading a book recently, but i do not understand with one thing.
this a structure.
Code:
Employee STRUCT
  IdNum           BYTE "000000000"
  LastName        BYTE 30 DUP(0)
  ALIGN           WORD
  Years           WORD 0
  ALIGN           DWORD
  SalaryHistory   DWORD 0,0,0,0
Employee ENDS
and given the definition:
Code:
worker Employee <>
and why it must be write like this?
Code:
mov esi,OFFSET worker
mov ax,(Employee PTR [esi]).Years
why it says "The following statement does not assemble because Years by itself does not identify the structure it belongs to:
Code:
mov ax,[esi].Years             ; invalid
my point is why there needs a Employee PTR in front of [esi]?
thanks so much!
 
Old 06-13-2011, 08:01 AM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
because ... uhh ... "ESI does not identify the structure it belongs to."

Consider this: if you did not know (because you had not been told) that ESI pointed to an Employee structure, how would you compute the proper offset to use for "Years?"

(Let's assume that there are twenty different structure definitions in this program, all of which define a field named "Years.")

The answer is: "you couldn't." Unless the programmer tells you what ESI is pointing to at any particular time, you (the assembler...) cannot solve the problem.
 
Old 06-13-2011, 08:23 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by topheraholic View Post
why there needs a Employee PTR in front of [esi]?
Because the fools who designed the language you're trying to use didn't understand why there was a difference between high level languages and assembly languages.

High level languages can support complicated scoping, such as multiple structs defined within the same scope containing the same named field. Such scoping is totally unnatural for ASM, and results in verbose unreadable code that defeats any purpose of writing ASM. If you want to write in a high level language, write in a high level language.
 
Old 06-13-2011, 09:52 AM   #4
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by sundialsvcs View Post
because ... uhh ... "ESI does not identify the structure it belongs to."

Consider this: if you did not know (because you had not been told) that ESI pointed to an Employee structure, how would you compute the proper offset to use for "Years?"

(Let's assume that there are twenty different structure definitions in this program, all of which define a field named "Years.")

The answer is: "you couldn't." Unless the programmer tells you what ESI is pointing to at any particular time, you (the assembler...) cannot solve the problem.
but this instruction
Code:
mov esi,OFFSET worker
and this one
Code:
.data
worker Employee <>
tells that worker is a structure variable,and it passes the address of worker structure.so i should know "Years" is belongs to "worker" structure.
 
Old 06-13-2011, 10:11 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by topheraholic View Post
but this instruction
Code:
mov esi,OFFSET worker
and this one
Code:
.data
worker Employee <>
tells that worker is a structure variable,and it passes the address of worker structure.so i should know "Years" is belongs to "worker" structure.
But there is no declaration of the data type of esi. I don't know if this stupid kludge of a language has a way to declare (rather than cast) the data type of a register. It is so far from what asm should be, that I don't want to know.

Without that, your code is equivalent this C++:

Code:
Employee worker;   // worker Employee <>
void* esi;
esi = &worker;  // mov esi,OFFSET worker
short ax;
ax = esi->Years;  // mov ax,[esi].Years
In that C++ code, the compiler could do flow analysis to know what kind of thing esi points to. But it isn't supposed to do that to resolve syntax/semantic issues, only for optimization. So the above code won't work.

In ASM, the assembler isn't supposed to do that flow analysis at all. So seeing mov esi,OFFSET worker doesn't make the assembler take that info about esi to any other line of your code. That info stays on that line only.

Last edited by johnsfine; 06-13-2011 at 10:15 AM.
 
Old 06-13-2011, 08:05 PM   #6
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Original Poster
Rep: Reputation: 15
Quote:
In ASM, the assembler isn't supposed to do that flow analysis at all. So seeing mov esi,OFFSET worker doesn't make the assembler take that info about esi to any other line of your code. That info stays on that line only.
but what does this instruction really do? nothing?
Code:
mov esi,OFFSET worker
and what is the value of esi in this instruction?
Code:
 mov ax,(Employee PTR [esi]).Years

Last edited by topheraholic; 06-13-2011 at 08:18 PM.
 
Old 06-13-2011, 09:02 PM   #7
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by johnsfine View Post
But there is no declaration of the data type of esi.
but esi points to the address of worker!so work.years could be works as well!but why it is not?
 
Old 06-14-2011, 05:47 AM   #8
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by topheraholic View Post
but what does this instruction really do? nothing?
Code:
mov esi,OFFSET worker
At least one of your other posts (not in this thread) implies you know some C. Do you know what this line of C does?
Code:
void* esi = &worker;
The asm instruction you asked about puts the address of worker into the register esi. It does not do anything with type information. The register esi holds only the address. It has no extra bits to know the data type of the thing it points to.

Even in C, a pointer holds just an address. At run time, the pointer holds no type information. Type checking in C is all done at compile time. If you wrote
Code:
Employee* esi = &worker;
in C, the compiler would know esi was an Employee* (because you declared it as such, not because you initialized it to point to worker) so later use of esi can take advantage of that compile time type knowledge.

Your actual instruction
Code:
mov esi,OFFSET worker
doesn't create any compile time type knowledge. At run time, esi gets just the address of worker, not any info about the type. At compile time, even less is remembered. The assembler generates the machine language instruction for that ASM instruction, but carries no knowledge of it forward to affect later instructions.

A human sees that instruction and knows what kind of thing esi points to and can apply that knowledge to help understand the next use of esi in the program. The computer does not use that kind of knowledge/understanding, neither at run time nor at assembly time.

Quote:
Originally Posted by topheraholic View Post
but esi points to the address of worker!so work.years could be works as well!but why it is not?
(only guessing at what you mean, so I might be answering the wrong question):

The assembler does know the type of worker, so it should be able to understand something like worker.Years

I don't know the exact syntax for that. I haven't used asm in quite a while and I never made significant use of the stupid features of Microsoft ASM that make it more like a high level language. So I'm pretty sure you could do something like
Code:
mov ax, worker.Years
and not need to involve esi at all. But I'm not sure of exact syntax.

Last edited by johnsfine; 06-14-2011 at 05:53 AM.
 
1 members found this post helpful.
Old 06-14-2011, 06:30 AM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
We have a saying in the Perl world: TMTOWTDI = There's More Than One Way To Do It ...
 
Old 06-16-2011, 07:01 AM   #10
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Original Poster
Rep: Reputation: 15
oh thanks very much! it helps a lot!
 
  


Reply



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
Hey guys! ozzy24 LinuxQuestions.org Member Intro 1 08-17-2010 12:42 PM
Hey, guys! goldwave LinuxQuestions.org Member Intro 1 07-16-2010 01:12 PM
Hey guys draft0r LinuxQuestions.org Member Intro 2 02-26-2007 07:55 PM
Hey Guys Frozen Gamez LinuxQuestions.org Member Intro 2 10-06-2003 10:18 PM

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

All times are GMT -5. The time now is 09:47 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