Author Topic: Hello World problems  (Read 6158 times)

Offline IronCanTaco

  • Jr. Member
  • *
  • Posts: 7
Hello World problems
« on: November 12, 2013, 10:09:24 PM »
I am seriously lost here guys.

I have done a simple Hello World program by a tutorial and tried experimenting with it, but every single thing that I do just crashes the whole thing.

This is my "Hello World" that is working:

Code: [Select]
bits 32
extern _printf
global _main

section .data
output: db "Hello World", 10, 0

section .text

_main:
pushad
push dword output
call _printf
add esp, 4
popad
ret

I am working on 64-bit Windows 8.

Now, question:

Why can't I do this:

Code: [Select]
section .data
output1: db "Hello World", 10, 0
output2: db "Hi", 10, 0
....

push dword output1
call _printf
push dword output2
call _printf

It just crashes. Why?

It also crashes it I try pausing it.
I have tried a lot of stuff but nothing seems to work, so I would please like somebody to explain to me how can I pause the program (console)

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Hello World problems
« Reply #1 on: November 12, 2013, 11:47:32 PM »
Well, from what you show, I would try...
Code: [Select]
push dword output1
call _printf
add esp, 4
push dword output2
call _printf
add esp, 4
; etc...

I don't know if that's the problem, but you don't show the "clean up the stack" part, which you need to do. You don't say what you tried to pause the program. I suppose "call _getc" would do it(?). The "pause" instruction, though it sounds encouraging, doesn't do what it promises - "hlt" won't work either.

You mention working on 64-bit Windows. What you show is 32-bit code. That "should" work (AFAIK), ASSuming that you've assembled it as "-f win32" and linked it "properly". Starting with "working" code and experimenting is a good way to proceed! You may need to post more complete "non-working" code and command lines for Nasm and linker or compiler before we can help you diagnose what's going wrong.

You're not lost, you're just taking the scenic route! :)

Best,
Frank


Offline IronCanTaco

  • Jr. Member
  • *
  • Posts: 7
Re: Hello World problems
« Reply #2 on: November 13, 2013, 10:02:30 AM »
Taking a scenic route. I like it :D

For printing out two lines cleaning up stack worked perfectly...

For the pause I used "call _getch". It finally works!!

How I create .o and .exe:

nasm -f win32 file.asm -o file.o
gcc file.o -o file

As far as I've read, it works because OS supports 32 and 64 bit, unlike some (OpenBSD IIRC)

I have one more question:

In line "output: db "outputt", 10,0" what is the point of "10,0"?
« Last Edit: November 13, 2013, 10:10:34 AM by IronCanTaco »

Offline 0xFF

  • Jr. Member
  • *
  • Posts: 15
Re: Hello World problems
« Reply #3 on: November 14, 2013, 05:03:51 AM »
All characters are actually stored as numbers, each character being represented by a single byte, meaning the values can range from 0-255, for a total  of 256 potential characters. The letters begin at 65, spaces are also a character, 32, and other formatting marks have their own special characters. Now line-feed is the term used for 'return' or 'enter', think a typewriter, where you hit the end, it goes 'bing', rolls the page down and moves to the left margin for a new line. Line feed's character number is 10. 0 is the null character, it's literally nothing. That's used as a demarkation point for many functions, such as printf, which yo could describe loosely as "Print this data, byte by byte, as a certain format, until you see a 0 character". So, you put "my message", 10, 0 to print your message, make a new line and stop. :)

I'm glad you solved your problems, but can you try one last version for me? Try doing it the original way, with push, printf, push, printf, and then after that clean up the stack with "add esp, 8" or even the very slick (this is useful if you've set up ebp properly) mov esp, ebp. I think the problem probably is, as I said, your final clean-up, where you probably left some data on the stack, other than ebp, so when you pop and return at the end you got a sysseg.

Offline IronCanTaco

  • Jr. Member
  • *
  • Posts: 7
Re: Hello World problems
« Reply #4 on: November 14, 2013, 08:32:42 AM »
Thank you for the explanation.

I tried it with add esp, 8 and I get an error.

Offline 0xFF

  • Jr. Member
  • *
  • Posts: 15
Re: Hello World problems
« Reply #5 on: November 14, 2013, 02:09:51 PM »
The study of how characters, colours, brightness, sound, all these things are represented in your computer is a very interesting subject. :P

For my understanding, can you please post the full source code for that version you just did,that doesn't work? If you are able to tell me what the error you receive actually says, I would be grateful.

Many thanks!