943,708 Members | Top Members by Rank

Ad:
  • Assembly Discussion Thread
  • Unsolved
  • Views: 1351
  • Assembly RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 16th, 2009
0

Assembly wierdness

Expand Post »
I am doing Narue's Introduction to Assembly, and when I run my program, nothing happens.

This is my code:
asm Syntax (Toggle Plain Text)
  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:
Assembly Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 9
Solved Threads: 5
Posting Pro
tomtetlaw is offline Offline
591 posts
since Sep 2008
Aug 17th, 2009
0

Re: Assembly wierdness

Can someone please help me?
Reputation Points: 9
Solved Threads: 5
Posting Pro
tomtetlaw is offline Offline
591 posts
since Sep 2008
Aug 17th, 2009
0

Re: Assembly wierdness

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.

Assembly Syntax (Toggle Plain Text)
  1. .start
  2. _main:
So the code knows where to begin execution!
Last edited by wildgoose; Aug 17th, 2009 at 4:42 am.
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Aug 17th, 2009
0

Re: Assembly wierdness

This is now my code:
asm Syntax (Toggle Plain Text)
  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:
Assembly Syntax (Toggle Plain Text)
  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.
Reputation Points: 9
Solved Threads: 5
Posting Pro
tomtetlaw is offline Offline
591 posts
since Sep 2008
Aug 17th, 2009
0

Re: Assembly wierdness

ok, sorry, here is my code again that still doesn't work:

asm Syntax (Toggle Plain Text)
  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.
Reputation Points: 9
Solved Threads: 5
Posting Pro
tomtetlaw is offline Offline
591 posts
since Sep 2008
Aug 17th, 2009
0

Re: Assembly wierdness

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.
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Aug 17th, 2009
0

Re: Assembly wierdness

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/
Reputation Points: 99
Solved Threads: 5
Junior Poster
Evenbit is offline Offline
140 posts
since Mar 2005
Aug 17th, 2009
0

Re: Assembly wierdness

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.
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Aug 17th, 2009
0

Re: Assembly wierdness

Click to Expand / Collapse  Quote originally posted by wildgoose ...
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.
Reputation Points: 99
Solved Threads: 5
Junior Poster
Evenbit is offline Offline
140 posts
since Mar 2005
Aug 17th, 2009
0

Re: Assembly wierdness

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.
Reputation Points: 99
Solved Threads: 5
Junior Poster
Evenbit is offline Offline
140 posts
since Mar 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Assembly Forum Timeline: Win32 functions in NASM
Next Thread in Assembly Forum Timeline: assembly switch/case macro





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC