Assembly wierdness

Reply

Join Date: Sep 2008
Posts: 369
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

Assembly wierdness

 
0
  #1
Aug 16th, 2009
I am doing Narue's Introduction to Assembly, and when I run my program, nothing happens.

This is my code:
  1. [section .data]
  2. hello: db 'Hello, world!', 10, 0 ;15 bytes
  3. nl: db ' ', 10, 0 ;3 bytes
  4.  
  5. [section .text]
  6. global _main, _print_nl, _print_msg, _return
  7. extern _printf
  8.  
  9. _print_nl:
  10. push nl
  11. call _printf
  12. add esp, 4
  13.  
  14. _print_msg:
  15. push hello
  16. call _printf
  17. add esp, 4
  18.  
  19. _return:
  20. mov eax, 0
  21. ret
  22.  
  23. _main:
  24. call _print_msg
  25. call _print_nl
  26. call _print_msg
  27.  
  28. call _return
This is what happened in my command window:
  1. C:\Documents and Settings\tom\Desktop\Assembly>nasm -f win32 First.asm -o First.obj
  2. C:\Documents and Settings\tom\Desktop\Assembly>gcc First.obj -o prog.exe
  3. C:\Documents and Settings\tom\Desktop\Assembly>prog.exe
  4. C:\Documents and Settings\tom\Desktop\Assembly>

Any help would be appreciated
Last edited by tomtetlaw; Aug 16th, 2009 at 4:04 am.
...
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 369
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

Re: Assembly wierdness

 
0
  #2
Aug 17th, 2009
Can someone please help me?
...
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Assembly wierdness

 
0
  #3
Aug 17th, 2009
MASM is my thing when linked with Visual Studio. Not NASM but I'll try to help...

Assemble First.asm into an Win32 object file First.obj
C:\Documents and Settings\tom\Desktop\Assembly>nasm -f win32 First.asm -o First.obj
Seems okay
C:\Documents and Settings\tom\Desktop\Assembly>gcc First.obj -o prog.exe


C:\Documents and Settings\tom\Desktop\Assembly>prog.exe
C:\Documents and Settings\tom\Desktop\Assembly>

Try moving the .data section below the .text (code) section. If the data is put into the code segment then the code is being displaced and the data is being treated as code. Dumping lst output or memory map would give more clues!

In masm one would define
end Start
at the end of the file to indicate where the code is to start. As the concept of 'main' is a C/C++ thing, not assembly language! It merely starts at the beginning of the code segment unless overwritten like with the end Main as indicated!

Looking at the NASM documentation, it appears you need to insert a declaration.

  1. .start
  2. _main:
So the code knows where to begin execution!
Last edited by wildgoose; Aug 17th, 2009 at 4:42 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 369
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

Re: Assembly wierdness

 
0
  #4
Aug 17th, 2009
This is now my code:
  1. [section .bss ]
  2. name: resb 64
  3.  
  4. [section .start]
  5. global _main, _print_nl, _print_msg, _return
  6. extern _printf, _read
  7. _main:
  8. push msg
  9. call _printf
  10. call _read
  11.  
  12. push hello
  13. call _printf
  14.  
  15. push eax
  16. call _printf
  17.  
  18. push exmrk
  19. call _printf
  20.  
  21. push nl
  22. call _printf
  23.  
  24. add esp, 20
  25.  
  26. mov eax, 0
  27. ret
  28.  
  29. [section .data]
  30. msg: db 'Enter your name: ',0;18 bytes
  31. hello: db 'Hello, ', 0 ;9 bytes
  32. nl: db ' ', 10, 0 ;3 bytes
  33. exmrk: db '!',0 ;2 bytes

_read DOES get input from the user and store it in eax right?

This is what happens when I run the thing:
  1. Exiting due to signal SIGSEGV
  2. Stack Fault at eip=00088a87
  3. eax=00000001 ebx=00000299 ecx=00000001 edx=0000033f esi=00000054 edi=0000fd7c
  4. ebp=0008fd70 esp=0008fd5c program=C:\DOCUME~1\TOM\DESKTOP\ASSEMBLY\FIRST.EXE
  5. cs: sel=01a7 base=02990000 limit=0009ffff
  6. ds: sel=01af base=02990000 limit=0009ffff
  7. es: sel=01af base=02990000 limit=0009ffff
  8. fs: sel=017f base=00005a80 limit=0000ffff
  9. gs: sel=01bf base=00000000 limit=0010ffff
  10. ss: sel=01af base=02990000 limit=0009ffff
  11. App stack: [0008fd7c..0000fd7c] Exceptn stack: [0000fcdc..0000dd9c]
  12.  
  13. Call frame traceback EIPs:
  14. 0x00088a87


help?
Last edited by tomtetlaw; Aug 17th, 2009 at 7:24 am.
...
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 369
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

Re: Assembly wierdness

 
0
  #5
Aug 17th, 2009
ok, sorry, here is my code again that still doesn't work:

  1. [section .data ]
  2. hello: db 'Hello, world!',10,0
  3.  
  4. [section .text ]
  5. global _main
  6. extern _printf
  7. .start
  8. _main:
  9. push hello
  10. call _printf
  11. add esp,4
  12.  
  13. mov eax,0
  14. ret
it doesn't do anything when i run it
any help?
Last edited by tomtetlaw; Aug 17th, 2009 at 8:35 am.
...
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Assembly wierdness

 
0
  #6
Aug 17th, 2009
For every push before a call, you have to adjust the stack.

You can't be throwing around code changes and hope it works. You had the code reasonable before. If you're going to make massive changes like that you need to indicate the changes worked, and then why you throwing away what you were helped with.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 129
Reputation: Evenbit is on a distinguished road 
Solved Threads: 4
Evenbit's Avatar
Evenbit Evenbit is offline Offline
Junior Poster

Re: Assembly wierdness

 
0
  #7
Aug 17th, 2009
Which gcc port are you using? If it is cygwin, then that might be your problem.

Try MingW -- I've always had success with it.

http://www.mingw.org/
while (CPU is present) {some assembly required}
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Assembly wierdness

 
0
  #8
Aug 17th, 2009
Assember is a two pass. If that doesn't work, move all the data in the .data section below the code in the .text section. In these simple configurations typically the Code Segment and Data Segment are shared and having data at the top of your code makes the data appear like opcodes (thus instructions) to the processor and it trys to run the data like code!

When you run it! So that would indicate you get a clean assemble and link and thus you are successful in linking to the libary that contains the print code.
Last edited by wildgoose; Aug 17th, 2009 at 12:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 129
Reputation: Evenbit is on a distinguished road 
Solved Threads: 4
Evenbit's Avatar
Evenbit Evenbit is offline Offline
Junior Poster

Re: Assembly wierdness

 
0
  #9
Aug 17th, 2009
Originally Posted by wildgoose View Post
Assember is a two pass. If that doesn't work, move all the data in the .data section below the code in the .text section. In these simple configurations typically the Code Segment and Data Segment are shared and having data at the top of your code makes the data appear like opcodes (thus instructions) to the processor and it trys to run the data like code!
No, wildgoose. What you are saying simply can not happen.

The sections are separated in the object file. The linker may even choose to re-order them when it creates the executable file. The Operating System's loader will likely scatter them into widely-separated areas of memory. In general, the order of .data and .text sections has no effect. The code will start executing at the '_main' label no matter where it appears within the .text section.

However, in a Linux environment, on certain old kernel versions, the order DOES matter. A '.data' section is expected as the last section due to security device implemented by that kernel.
Last edited by Evenbit; Aug 17th, 2009 at 6:46 pm.
while (CPU is present) {some assembly required}
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 129
Reputation: Evenbit is on a distinguished road 
Solved Threads: 4
Evenbit's Avatar
Evenbit Evenbit is offline Offline
Junior Poster

Re: Assembly wierdness

 
0
  #10
Aug 17th, 2009
wildgoose -

What you are thinking of is the old '*.com' and '*.exe' formats for DOS which had a segmented memory model. Yes, the '*.com' files were restricted to the SMALL data model which combined all four segments into one -- so the author *did* have to be careful where he place data.

However, modern 32-bit code for modern 32-bit Operating System's do not have support for segments because they all use Virtual Memory.
while (CPU is present) {some assembly required}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC