Introduction: Programming in C with Assembler Language


HELLO WORLD IN C AND ASSEMBLER


Here is our first program in C, notice the program begins with a comment
enclosed within /* comment */ (a slash and a star), these are ignored
by the compiler. The real program begins with main(), puts("") is used
to output a string (enclosed in "" quotes) to the screen.

/*(c)1996 DAVID DALBY. */

/* TUTOR L1.0 */

main()
{
        /* this is a comment */
        puts("Hello World");
        puts("This is my first program");
}

Here is the Assembler Program for the above C program. As you can see
it is longer, also the instructions are becoming less like the English
language all the time, this is known as LOW LEVEL PROGRAMMING.

CSEG means CODE SEGMENT, given the address in memory of where the
code is for the main program and the output routine (puts).

DSEG means DATA SEGMENT, which is where the data is stored in memory.
__3 and __4 are LABELS which tell the program where to find the string
in memory.

DB means DEFINE BYTE, the bytes being defined are the actual strings
to be printed to the screen. These are usually enclosed within '' and
terminate with a 0, (called zero terminated strings).

Comments in assembler are usually with a ; (semi-colon).

Here is the commented assembler code for the C program.

CSEG
 PUBLIC main_
 PUBLIC puts_
 DSEG
__3 DB 'Hello World',0
__4 DB 'This is my first program',0
 CSEG

main_: PUSH BP           ;save BP
 MOV BP,SP               ;save current position of SP (stack Pointer)
 MOV AX,OFFSET __3       ;get the address in memory of first string
 PUSH AX                 ;save that address, due to enter subroutine
 CALL puts_              ;EXECUTE the PUTS subroutine, print the string
 MOV SP,BP               ;get original SP back
 MOV AX,OFFSET __4       ;get address of next string to be printed
 PUSH AX                 ;save address for the subroutine to use
 CALL puts_              ;PRINT THE STRING
 MOV SP,BP               ;get original SP back
 POP BP                  ;restore original BP so everything is unchanged
 RET                     ;return back to operating system or main program
 END                     ;end of program








NEXT LESSION COMING SOON, WATCH THIS SPACE



(c)1998 David A Dalby