Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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???

like image 405
vs4vijay Avatar asked Feb 01 '10 18:02

vs4vijay


People also ask

What is inline assembly explain with an example?

In computer programming, an inline assembler is a feature of some compilers that allows low-level code written in assembly language to be embedded within a program, among code that otherwise has been compiled from a higher-level language such as C or Ada.

Does C support inline assembly?

Inline assembly (typically introduced by the asm keyword) gives the ability to embed assembly language source code within a C program. Unlike in C++, inline assembly is treated as an extension in C.

What is asm () in C?

The asm keyword allows you to embed assembler instructions within C code. GCC provides two forms of inline asm statements. A basic asm statement is one with no operands (see Basic Asm), while an extended asm statement (see Extended Asm) includes one or more operands.


2 Answers

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...

like image 123
egrunin Avatar answered Sep 22 '22 14:09

egrunin


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.

like image 37
GManNickG Avatar answered Sep 22 '22 14:09

GManNickG