a Simple "Hello World" Inline Assembly language Program in C/C++

10,426

Solution 1

I don't have the Borland compiler on hand, so I might be misremembering its syntax, but have you tried this:

asm {
    mov ax,4       // (I/O Func.)
    mov bx,1       // (Output func)  
    lds cx,"Hello, world" // (address of the string)
    mov dx,6       //  (length of the string)
    int 0x21       // system call
}

or this:

char msg[] = "Hello, world";

asm {
    mov ax,4       // (I/O Func.)
    mov bx,1       // (Output func)  
    lds cx, msg   // (address of the string)
    mov dx,6       //  (length of the string)
    int 0x21       // system call
}

edit: although this will compile (now that I've changed MOV to LDS), it will still throw an error at runtime. I'll try again...

Solution 2

Just put the variable name in there:

mov ax,4       // (I/O Func.)
mov bx,1       // (Output func)  
mov cx,name   // (address of the string)
mov dx,6       //  (lenght of the string)
int 0x21       // system call

Disclaimer: I'm not too good at assembly.

Share:
10,426
vs4vijay
Author by

vs4vijay

I am Vijay Soni, A Computer Science & Engineering Graduate, Coder, Gamer, Hacker, Thinker, Polyglot, Linuxer, Open Source Enthusiast. fork me on GitHub, view my gists and follow me @vs4vijay

Updated on June 21, 2022

Comments

  • vs4vijay
    vs4vijay almost 2 years

    i use devcpp and borland c compiler....

    asm {
        mov ax,4       // (I/O Func.)
        mov bx,1       // (Output func)  
        mov cx,&name   // (address of the string)
        mov dx,6       // (length of the string)
        int 0x21       // system call
    }
    

    in the above code snippets i want to print a string with the help of assembly language... but how can i put the address of the string in register cx....

    is there something wrong in code???