Author Topic: Why does my hello world program in assembly language keeps crashing?  (Read 24228 times)

Offline sunshine33

  • Jr. Member
  • *
  • Posts: 23
  • Country: iq
    • @pump_upp - best crypto pumps on telegram !
Code: [Select]
%include "io.inc"

    section .text
    global CMAIN
    CMAIN:
        section .text
        global _start       ;must be declared for using gcc
    _start:                     ;tell linker entry point
        mov edx, len    ;message length
        mov ecx, msg    ;message to write
        mov ebx, 1      ;file descriptor (stdout)
        mov eax, 4      ;system call number (sys_write)
        int 0x80        ;call kernel
        mov eax, 1      ;system call number (sys_exit)
        int 0x80        ;call kernel

    section .data

    msg db  'Hello, world!',0xa ;our dear string
    len equ $ - msg         ;length of our dear string
        xor eax, eax
        ret

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Why does my hello world program in assembly language keeps crashing?
« Reply #1 on: September 16, 2017, 05:49:36 PM »
Hi sunshine33,

Welcome to the Forum.

I don't know why your program should be crashing. It looks a little "odd" to me, but I don't see why it should crash. Let us rule out one possibility first. You're not trying to run this in Windows, are you? That's Linux code, and definitely won't run in Windows (or MACOS or DOS or BSD, etc.).

The "ret" - last line in your code - would crash, but I don't think it's ever executed. The "_start:" label is not "call"ed so you can't "ret" from it. The first thing on your stack is "argc". In any case, code doesn't belong in "section .data".

What's in "io.inc"? I thought at first that was the late Sivarama Dandimudi's, but no, his is "io.mac". Dr. Carter's is "asm_io.inc". I don't think I recognise that one.

What's "CMAIN"? Are you linking this with some other code? As a general rule, you want either "_start" or "main" - not both... but there could be exceptions...

We may need more information before we can help you with this.

Best,
Frank


Offline sunshine33

  • Jr. Member
  • *
  • Posts: 23
  • Country: iq
    • @pump_upp - best crypto pumps on telegram !
Re: Why does my hello world program in assembly language keeps crashing?
« Reply #2 on: September 17, 2017, 03:51:57 AM »
Thanks for the reply frank .

Yes i am trying to run this in windows using the SASM Assembler .

https://dman95.github.io/SASM/english.html

I want to run this code in windows itself . I don't feel like changing to Linux just for the purpose of learning Assembly language .

The only working hello world program i have seen  is this one here .

https://www.tutorialspoint.com/compile_assembly_online.php

Please help


Offline sunshine33

  • Jr. Member
  • *
  • Posts: 23
  • Country: iq
    • @pump_upp - best crypto pumps on telegram !
Re: Why does my hello world program in assembly language keeps crashing?
« Reply #3 on: September 17, 2017, 04:16:47 AM »
This program works in SASM :-)

Code: [Select]
%include "io.inc"

section .data
    msg db 'Hello, world!', 0

section .text
    global CMAIN
CMAIN:
    mov ebp, esp
    PRINT_STRING msg
    NEWLINE
    xor eax, eax
    ret

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Why does my hello world program in assembly language keeps crashing?
« Reply #4 on: September 17, 2017, 05:31:43 AM »
Well, that's a start. At least it's something intended for Windows! You're not the first one to try to run that TutorialsPoint code on Windows. If you read closely, it does say that it's for Linux, but they don't make it very clear that it won't work for Windows. I hope you'll have better luck with SASM - can't get much worse. :)

Best,
Frank


Offline sunshine33

  • Jr. Member
  • *
  • Posts: 23
  • Country: iq
    • @pump_upp - best crypto pumps on telegram !
Re: Why does my hello world program in assembly language keeps crashing?
« Reply #5 on: September 17, 2017, 07:16:25 AM »
Thanks a lot again for the reply .

I am happy that i got at least one Assembly language program to work in my SASM Assembler .

I am going to stick with SASM , because it looks good and i  like it . :-)

I wish i could find more simple examples to work with .

Any place where i can find some examples of Assembly language programs ?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Why does my hello world program in assembly language keeps crashing?
« Reply #6 on: September 17, 2017, 08:24:23 AM »
To be clear about it, SASM is not an assembler, it is an "IDE" - an "integrated development environment". It provides an editor, any one of several assemblers, a linker, and a debugger all in one package. Their "io.inc" looks useful, too. Many people find it a convenient way to program.

You can find examples of assembly language programs right here in the "Examples" section. However... They are not necessarily "simple", not necessarily suitable for beginners, Windows and Linux are mixed... generally not well "indexed". You might do well to start at the "high numbered" pages and work backwards, to find the "simpler" stuff.

I wonder if there are more examples around the SASM pages somewhere? "io.inc" includes some macros which should make it easy to do "Please tell me your name" "Hello uhClem" and "The answer is 42" and stuff.

It's a shame that desperate beginners keep finding that Linux code! For reference, "int 80h" is Linux. Windows uses "API"s (application programming interface). You might see...
Code: [Select]
extern ExitProcess
extern MessageBoxA
extern GetStdHandle
extern WriteFile
extern RegisterClassEx
things like that...

Best,
Frank


Offline sunshine33

  • Jr. Member
  • *
  • Posts: 23
  • Country: iq
    • @pump_upp - best crypto pumps on telegram !
Re: Why does my hello world program in assembly language keeps crashing?
« Reply #7 on: September 17, 2017, 11:12:27 AM »
Thanks a lot :-)

I really love to learn from examples , right now i am not capable of coding assembly language from scratch .

This is the only way i am going to learn it .

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Why does my hello world program in assembly language keeps crashing?
« Reply #8 on: September 17, 2017, 12:49:42 PM »
I'm pretty fond of examples, too. But sometimes documentation is helpful, too. For example:
https://docs.microsoft.com/en-us/windows/console/readconsole

Best,
Frank


Offline sunshine33

  • Jr. Member
  • *
  • Posts: 23
  • Country: iq
    • @pump_upp - best crypto pumps on telegram !
Re: Why does my hello world program in assembly language keeps crashing?
« Reply #9 on: September 21, 2017, 05:29:31 AM »
Sorry for the delay .
I was a bit busy collecting appropriate examples for my level .

I found three for my needs .

Linux ASM example

Code: [Select]
section .text
   global _start ;must be declared for linker (gcc)

_start:          ;tell linker entry point
   mov edx,len  ;message length
   mov ecx,msg  ;message to write
   mov ebx,1    ;file descriptor (stdout)
   mov eax,4    ;system call number (sys_write)
   int 0x80     ;call kernel

   mov edx,9    ;message length
   mov ecx,s2   ;message to write
   mov ebx,1    ;file descriptor (stdout)
   mov eax,4    ;system call number (sys_write)
   int 0x80     ;call kernel

   mov eax,1    ;system call number (sys_exit)
   int 0x80     ;call kernel

section .data
msg db 'Displaying 9 stars',0xa ;a message
len equ $ - msg  ;length of message
s2 times 9 db '*'



Hello world windows

Code: [Select]
%include "io.inc"

section .data
    msg db 'Hello, world!', 0

section .text
    global CMAIN
CMAIN:
    mov ebp, esp
    PRINT_STRING msg
    NEWLINE
    xor eax, eax
    ret

Hello world 2 ; windows

Code: [Select]
%include "io.inc"

section .text
global CMAIN
CMAIN:
    mov ebp, esp; for correct debugging
  ; ----------------------------------------------------------------------------
; hello.asm
;
; This is a Win32 console program that writes "Hello, World" on one line and
; then exits.  It uses only plain Win32 system calls from kernel32.dll, so it
; is very instructive to study since it does not make use of a C library.
; Because system calls from kernel32.dll are used, you need to link with
; an import library.  You also have to specify the starting address yourself.
;
; Assembler: NASM
; OS: Any Win32-based OS
; Other libraries: Use gcc's import library libkernel32.a
; Assemble with "nasm -fwin32 hello.asm"
; Link with "ld -e go hello.obj -lkernel32"
; ----------------------------------------------------------------------------

        global  go
        extern  _ExitProcess@4
        extern  _GetStdHandle@4
        extern  _WriteConsoleA@20
        extern  _ReadConsoleA@20

        section .data
msg:    db      'Hello, World', 10
handle:         dd      0
read_handle dd 0
written:        db      0

        section .text
go:
        ; handle = GetStdHandle(-11)
        push    dword -11
        call    _GetStdHandle@4
        mov     [handle], eax

        push -10  ; stdin
        call _GetStdHandle@4
        mov [read_handle], eax

        ; WriteConsole(handle, &msg[0], 13, &written, 0)
        push    dword 0
        push    written
        push    dword 13
        push    msg
        push    dword [handle]
        call    _WriteConsoleA@20

        push eax
        mov eax, esp ; buffer for char?
        push 0
        push written ; reuse this?
        push 1 ; characters to read?
        push eax
        push dword [read_handle]
        call  _ReadConsoleA@20
        pop eax ; character read in al?

        ; ExitProcess(0)
        push    dword 0
        call    _ExitProcess@4
    xor eax, eax
    ret

Reading a couple of books too :-)

Offline crasher32

  • New Member
  • Posts: 1
Re: Why does my hello world program in assembly language keeps crashing?
« Reply #10 on: April 21, 2020, 05:01:25 PM »
Code: [Select]
%include "io.inc"

    section .text
    global CMAIN
    CMAIN:
        section .text
        global _start       ;must be declared for using gcc
    _start:                     ;tell linker entry point
        mov edx, len    ;message length
        mov ecx, msg    ;message to write
        mov ebx, 1      ;file descriptor (stdout)
        mov eax, 4      ;system call number (sys_write)
        int 0x80        ;call kernel
        mov eax, 1      ;system call number (sys_exit)
        int 0x80        ;call kernel

    section .data

    msg db  'Hello, world!',0xa ;our dear string
    len equ $ - msg         ;length of our dear string
        xor eax, eax
        ret



its because the second _start: you made , this line

_start:                     ;tell linker entry point

delete it and it will work , and always read the error messages , it says (multiple definition of `_start') , cheers