<- previous    index    next ->

Lecture 2 Getting and using NASM

NASM is installed on  linux.gl.umbc.edu  and can be used there.

From anywhere that you can reach the internet, log onto your
UMBC account using:

    ssh  your-user-id@linux.gl.umbc.edu
    your-password


You should set up a directory for CMSC 313 and keep all your
course work in one directory.

  e.g.    mkdir  cs313  # only once
          cd  cs313

Copy over a sample program to your directory using:

 cp /afs/umbc.edu/users/s/q/squire/pub/../download/hello.asm  .


Assemble hello.asm using:

  nasm -f elf hello.asm

Link to create an executable using:

  gcc -o hello  hello.o

Execute the program using:

  hello

Now look at the file hello.asm

;  hello.asm  a first program for nasm for Linux, Intel, gcc
;
; assemble:	nasm -f elf -l hello.lst  hello.asm
; link:		gcc -o hello  hello.o
; run:	        hello 
; output is:	Hello World 

	SECTION .data		; data section
msg:	db "Hello World",10	; the string to print, 10=lf or '\n'
len:	equ $-msg		; "$" means "here"
				; len is a value, not an address

	SECTION .text		; code section
        global main		; make label available to linker 
main:				; standard  gcc  entry point
	
	mov	edx,len		; arg3, length of string to print
	mov	ecx,msg		; arg2, pointer to string
	mov	ebx,1		; arg1, where to write, screen
	mov	eax,4		; write command to int 80 hex
	int	0x80		; interrupt 80 hex, call kernel
	
	mov	ebx,0		; exit code, 0=normal
	mov	eax,1		; exit command to kernel
	int	0x80		; interrupt 80 hex, call kernel


There can be many types of data in the  ".data" section:
Look at the file testdata.asm
and see the results in testdata.lst

;  testdata.asm  a program to demonstrate data types and values
;
; assemble:	nasm -f elf -l testdata.lst  testdata.asm
; link:		gcc -o testdata  testdata.o
; run:	        testdata 
; Look at the list file, testdata.lst

; Note! nasm ignores the type of data and type of reserved
; space when used as memory addresses.
; You may have to use qualifiers BYTE, WORD or DWORD [dd01]

	
	section .data		; data section
				; initialized, writeable

				; db for data byte, 8-bit 
db01:	db	255,1,17	; decimal values for bytes
db02:	db	0xff,0ABh	; hexadecimal values for bytes
db03:	db	'a','b','c'	; character values for bytes
db04:	db	"abc"		; string value as bytes 'a','b','c'
db05:	db	'abc'		; same as "abc" three bytes
db06:	db	"hello",13,10,0 ; "C" string including cr and lf

				; dw for data word, 16-bit
dw01:	dw	12345,-17,32	; decimal values for words
dw02:	dw	0xFFFF,0abcdH	; hexadecimal values for words
dw03:	dw	'a','ab','abc'	; character values for words
dw04:	dw	"hello"		; three words, 6-bytes allocated

				; dd for data double word, 32-bit
dd01:	dd	123456789,-7	; decimal values for double words
dd02:	dd	0xFFFFFFFF	; hexadecimal value for double words
dd03:	dd	'a'		; character value in double word
dd04:	dd	"hello"		; string in two double words
dd05:	dd	13.27E30	; floating point value 32-bit IEEE

				; dq for data quad word, 64-bit
dq01:	dq	13.27E300	; floating point value 64-bit IEEE

				; dt for data ten of 80-bit floating point
dt01:	dt	13.270E3000	; floating point value 80-bit in register


	
	section .bss		; reserve storage space
				; uninitialized, writeable
	
s01:	resb	10		; 10 8-bit bytes reserved
s02:	resw	20		; 20 16-bit words reserved
s03:	resd	30		; 30 32-bit double words reserved
s04:	resq	40		; 40 64-bit quad words reserved
s05:	resb	1		; one more byte
	
	SECTION .text		; code section
        global main		; make label available to linker 
main:				; standard  gcc  entry point

	
	mov	al,[db01]	; correct to load a byte
	mov	ah,[db01]	; correct to load a byte
	mov	ax,[dw01]	; correct to load a word
	mov	eax,[dd01]	; correct to load a double word

	mov	al,BYTE [db01]	; redundant, yet allowed

	mov	ax,[db01]	; no warning, loads two bytes
	mov	eax,[dw01]	; no warning, loads two words

;	mov	ax,BYTE [db01]	; error, size miss match
;	mov	eax,WORD [dw01]	; error, size miss match

;	push	BYTE [db01]	; error, can not push a byte
	push	WORD [dw01]	; "push" needs to know size 2-byte
	push	DWORD [dd01]	; "push" needs to know size 4-byte
;	push	QWORD [dq01]	; error, can not push a quad word
	push	DWORD [dq01+4]	; push a floating point, half of it
	push	DWORD [dq01]	; push other half of floating point
	fld	DWORD [dd05]	; floating load 32-bit
	fld	QWORD [dq01]	; floating load 64-bit
		
	mov	ebx,0		; exit code, 0=normal
	mov	eax,1		; exit command to kernel
	int	0x80		; interrupt 80 hex, call kernel

; end testdata.asm

Now, see the values in testdata.lst (widen your window)

     1                                  ;  testdata.asm  program to demonstrate data types and values
     2                                  ;
     3                                  ; assemble:	nasm -f elf -l testdata.lst  testdata.asm
     4                                  ; link:		gcc -o testdata  testdata.o
     5                                  ; run:	        testdata 
     6                                  ; Look at the list file, testdata.lst
     7                                  
     8                                  ; Note! nasm ignores the type of data and type of reserved
     9                                  ; space when used as memory addresses.
    10                                  ; You may have to use qualifiers BYTE, WORD or DWORD [dd01]
    11                                  
    12                                  	
    13                                  	section .data		; data section
    14                                  				; initialized, writeable
    15                                  
    16                                  				; db for data byte, 8-bit 
    17 00000000 FF0111                  db01:	db	255,1,17	; decimal values for bytes
    18 00000003 FFAB                    db02:	db	0xff,0ABh	; hexadecimal values for bytes
    19 00000005 616263                  db03:	db	'a','b','c'	; character values for bytes
    20 00000008 616263                  db04:	db	"abc"		; string value as bytes 'a','b','c'
    21 0000000B 616263                  db05:	db	'abc'		; same as "abc" three bytes
    22 0000000E 68656C6C6F0D0A00        db06:	db	"hello",13,10,0 ; "C" string including cr and lf
    23                                  
    24                                  				; dw for data word, 16-bit
    25 00000016 3930EFFF2000            dw01:	dw	12345,-17,32	; decimal values for words
    26 0000001C FFFFCDAB                dw02:	dw	0xFFFF,0abcdH	; hexadecimal values for words
    27 00000020 6100616261626300        dw03:	dw	'a','ab','abc'	; character values for words
    28 00000028 68656C6C6F00            dw04:	dw	"hello"		; three words, 6-bytes allocated
    29                                  
    30                                  				; dd for data double word, 32-bit
    31 0000002E 15CD5B07F9FFFFFF        dd01:	dd	123456789,-7	; decimal values for double words
    32 00000036 FFFFFFFF                dd02:	dd	0xFFFFFFFF	; hexadecimal value for double words
    33 0000003A 61000000                dd03:	dd	'a'		; character value in double word
    34 0000003E 68656C6C6F000000        dd04:	dd	"hello"		; string in two double words
    35 00000046 AF7D2773                dd05:	dd	13.27E30	; floating point value 32-bit IEEE
    36                                  
    37                                  				; dq for data quad word, 64-bit
    38 0000004A C86BB752A7D0737E        dq01:	dq	13.27E300	; floating point value 64-bit IEEE
    39                                  
    40                                  				; dt for data ten of 80-bit floating point
    41 00000052 4011E5A59932D5B6F0-     dt01:	dt	13.270E3000	; floating point value 80-bit in register
    42 0000005B 66                 
    43                                  
    44                                  
    45                                  	
    46                                  	section .bss		; reserve storage space
    47                                  				; uninitialized, writeable
    48                                  	
    49 00000000           s01:	resb	10		; 10 8-bit bytes reserved
    50 0000000A           s02:	resw	20		; 20 16-bit words reserved
    51 00000032           s03:	resd	30		; 30 32-bit double words reserved
    52 000000AA           s04:	resq	40		; 40 64-bit quad words reserved
    53 000001EA           s05:	resb	1		; one more byte
    54                                  	
    55                                  	SECTION .text		; code section
    56                                          global main		; make label available to linker 
    57                                  main:				; standard  gcc  entry point
    58                                  
    59                                  	
    60 00000000 A0[00000000]            	mov	al,[db01]	; correct to load a byte
    61 00000005 8A25[00000000]          	mov	ah,[db01]	; correct to load a byte
    62 0000000B 66A1[16000000]          	mov	ax,[dw01]	; correct to load a word
    63 00000011 A1[2E000000]            	mov	eax,[dd01]	; correct to load a double word
    64                                  
    65 00000016 A0[00000000]            	mov	al,BYTE [db01]	; redundant, yet allowed
    66                                  
    67 0000001B 66A1[00000000]          	mov	ax,[db01]	; no warning, loads two bytes
    68 00000021 A1[16000000]            	mov	eax,[dw01]	; no warning, loads two words
    69                                  
    70                                  ;	mov	ax,BYTE [db01]	; error, size miss match
    71                                  ;	mov	eax,WORD [dw01]	; error, size miss match
    72                                  
    73                                  ;	push	BYTE [db01]	; error, can not push a byte
    74 00000026 66FF35[16000000]        	push	WORD [dw01]	; "push" needs to know size 2-byte
    75 0000002D FF35[2E000000]          	push	DWORD [dd01]	; "push" needs to know size 4-byte
    76                                  ;	push	QWORD [dq01]	; error, can not push a quad word
    77 00000033 FF35[4E000000]          	push	DWORD [dq01+4]	; push a floating point, half of it
    78 00000039 FF35[4A000000]          	push	DWORD [dq01]	; push other half of floating point
    79 0000003F D905[46000000]          	fld	DWORD [dd05]	; floating load 32-bit
    80 00000045 DD05[4A000000]          	fld	QWORD [dq01]	; floating load 64-bit
    81                                  		
    82 0000004B BB00000000              	mov	ebx,0		; exit code, 0=normal
    83 00000050 B801000000              	mov	eax,1		; exit command to kernel
    84 00000055 CD80                    	int	0x80		; interrupt 80 hex, call kernel
    85                                  
    86                                  ; end testdata.asm


    <- previous    index    next ->

Other links

Go to top