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
/*
* Very simple program to find prime numbers.
* (c)1996 david dalby
*
*/
#include
#define MAXPRIME 1000 /* Search up to here */
/*
* Main (and only) function
*/
main()
{
int num, test, limit;
char flag;
puts("Prime Finder by David Dalby");
for(num=1; num < MAXPRIME; num += 2) { /* Test range */
limit = num/2; /* Only test to here */
flag = 1; /* Assume prime */
for(test = 2; test <= limit; ++test) { /* Test for factors */
if(!(num%test)) { /* No remainder: factor */
flag = 0; /* Indicate not prime */
break; } } /* Waste no more time */
if(flag) /* Prime number, display */
printf("%d\n", num); }
}
;here is the assembly language code for the prime C programme
;see if you can follow along.
CSEG ;code segment here
PUBLIC main_
PUBLIC puts_
DSEG ;data segment here
__3 DB 'Prime Finder by David Dalby',0 ;zero terminated string
CSEG
PUBLIC printf_
DSEG
__5 DB '%d',10,0
CSEG
main_: PUSH BP ;start of program
MOV BP,SP
SUB SP,8
MOV AX,OFFSET __3 ;string pointer
PUSH AX ;save it
CALL puts_ ;print it subroutine
ADD SP,2
MOV WORD [BP-2],1 ;for loop start pointer
_L1: ;FOR LOOP STARTS HERE
CMP WORD [BP-2],1000 ;for loop end condition
JGE _L2 ;is num greater than 1000, yes exit loop
MOV AX,WORD [BP-2] ;otherwise execute loop contents
SAR AX,1
MOV WORD [BP-6],AX
MOV BYTE [BP-7],1
MOV WORD [BP-4],2
_L4:
MOV AX,WORD [BP-4]
CMP AX,WORD [BP-6]
JG _L5
MOV AX,WORD [BP-2]
CWD
IDIV WORD [BP-4]
CMP DX,0
JNZ _L7
MOV BYTE [BP-7],0
JMP _L5
_L7:
_L6:
INC WORD [BP-4]
JMP _L4
_L5:
CMP BYTE [BP-7],0
JZ _L8
PUSH WORD [BP-2]
MOV AX,OFFSET __5
PUSH AX
CALL printf_
ADD SP,4
_L8:
_L3:
ADD WORD [BP-2],2
JMP _L1
_L2:
MOV SP,BP
POP BP
RET
END
NEXT LESSION COMING SOON, WATCH THIS SPACE
(c)1998 David A Dalby