Skip to content

Commit

Permalink
add procedure example
Browse files Browse the repository at this point in the history
  • Loading branch information
datawolf committed Nov 7, 2013
1 parent 3a00207 commit 18b30b0
Show file tree
Hide file tree
Showing 9 changed files with 410 additions and 4 deletions.
10 changes: 6 additions & 4 deletions nasm/10/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Makefile for nasm program
all:binchar hex1char
all:binchar hex1char hex2char

ARCH=$(shell uname -m)

Expand All @@ -11,23 +11,25 @@ NASM_FLAGS=-f elf
LD_FLAGS=
endif


binchar: io.o binchar.o
ld $(LD_FLAGS) -o $@ $^

binchar.o: binchar.asm
nasm $(NASM_FLAGS) $<


hex1char: io.o hex1char.o
ld $(LD_FLAGS) -o $@ $^

hex1char.o: hex1char.asm
nasm $(NASM_FLAGS) $<

hex2char: io.o hex2char.o
ld $(LD_FLAGS) -o $@ $^

hex2char.o: hex2char.asm
nasm $(NASM_FLAGS) $<

.PHONY: clean

clean:
$(RM) binchar binchar.o hex1char hex1char.o
$(RM) binchar binchar.o hex1char hex1char.o hex2char hex2char.o
46 changes: 46 additions & 0 deletions nasm/10/hex2char.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
; Hex equivalent of character HEX2CHAR.ASM
;
; Objective: To print the hex equivalent of
; ASCII character code, Demonstrates
; the use of xlat instruction
; Input: Request a character from the user.
; Output: Prints the ASCII code of the input
; character in hex.

%include "io.mac"

.DATA
char_prompt db "Please input a character: ",0
out_msg1 db "The ASCII code of '",0
out_msg2 db "' in hex is ",0
query_msg db "Do you want to quit(Y/N): ",0
; translation table: 4 bit binary to hex
hex_table db "0123456789ABCDEF"
.UDATA

.CODE
.STARTUP
read_char:
PutStr char_prompt ; request a char, input
GetCh AL ; read input character
PutStr out_msg1
PutCh AL
PutStr out_msg2
mov AH,AL ; save input character in AH
mov EBX,hex_table ; EBX = translation table
shr AL,4 ; move upper 4 bits to lower half
xlatb ; replace AL with hex digit
PutCh AL ; wirte the first hex digit
mov AL, AH ; restore input character in AL
and AL, 0FH ; mask off the upper half byte
xlatb
PutCh AL ; wirte the second hex digit
nwln
PutStr query_msg ; query user whether to terminate
GetCh AL ; read response

cmp AL, 'Y' ; if response is 'Y'
jne read_char ; read another character
done: ; otherwise, terminate program
.EXIT
41 changes: 41 additions & 0 deletions nasm/11/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Makefile for nasm program
all:procex1 procex2 procex3 procswap

ARCH=$(shell uname -m)

ifeq ($(ARCH),x86_64)
NASM_FLAGS=-f elf32
LD_FLAGS=-m elf_i386
else
NASM_FLAGS=-f elf
LD_FLAGS=
endif

procex1: io.o procex1.o
ld $(LD_FLAGS) -o $@ $^

procex1.o: procex1.asm
nasm $(NASM_FLAGS) $<

procex2: io.o procex2.o
ld $(LD_FLAGS) -o $@ $^

procex2.o: procex2.asm
nasm $(NASM_FLAGS) $<

procex3: io.o procex3.o
ld $(LD_FLAGS) -o $@ $^

procex3.o: procex3.asm
nasm $(NASM_FLAGS) $<

procswap: io.o procswap.o
ld $(LD_FLAGS) -o $@ $^

procswap.o: procswap.asm
nasm $(NASM_FLAGS) $<

.PHONY: clean

clean:
$(RM) procex1 procex1.o procex2 procex2.o procex3 procex3.o procswap.o procswap
150 changes: 150 additions & 0 deletions nasm/11/io.mac
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
extern proc_nwln, proc_PutCh, proc_PutStr
extern proc_GetStr, proc_GetCh
extern proc_PutInt, proc_GetInt
extern proc_PutLInt, proc_GetLInt

;;-------------------------------------------------------------------
%macro .STARTUP 0
;group dgroup .data .bss
global _start
_start:
%endmacro
;;-------------------------------------------------------------------


;;-------------------------------------------------------------------
%macro .EXIT 0
mov EAX,1
xor EBX,EBX
int 0x80
%endmacro
;;-------------------------------------------------------------------


;;-------------------------------------------------------------------
%macro .DATA 0
segment .data
%endmacro
;;-------------------------------------------------------------------

;;-------------------------------------------------------------------
%macro .UDATA 0
segment .bss
%endmacro
;;-------------------------------------------------------------------

;;-------------------------------------------------------------------
%macro .CODE 0
segment .data
segment .bss
segment .text
%endmacro
;;-------------------------------------------------------------------


;;-------------------------------------------------------------------
%macro nwln 0
call proc_nwln
%endmacro
;;-------------------------------------------------------------------


;;-------------------------------------------------------------------
%macro PutCh 1
push AX
mov AL,%1
call proc_PutCh
pop AX
%endmacro
;;-------------------------------------------------------------------


;;-------------------------------------------------------------------
%macro PutStr 1
push ECX
mov ECX,%1
call proc_PutStr
pop ECX
%endmacro
;;-------------------------------------------------------------------


;;-------------------------------------------------------------------
%macro GetStr 1-2 81
push ESI
push EDI
mov EDI,%1
mov ESI,%2
call proc_GetStr
pop EDI
pop ESI
%endmacro
;;-------------------------------------------------------------------


;;-------------------------------------------------------------------
%macro GetCh 1
push SI
xor SI,SI
%ifidni %1,AL
;inc SI
call proc_GetCh
%elifidni %1,AH
mov SI,1
call proc_GetCh
%else
push AX
call proc_GetCh
mov %1,AL
pop AX
%endif
pop SI
%endmacro
;;-------------------------------------------------------------------


;;-------------------------------------------------------------------
%macro PutInt 1
push AX
mov AX,%1
call proc_PutInt
pop AX
%endmacro
;;-------------------------------------------------------------------


;;-------------------------------------------------------------------
%macro GetInt 1
%ifnidni %1,AX
push AX
call proc_GetInt
mov %1,AX
pop AX
%else
call proc_GetInt
%endif
%endmacro
;;-------------------------------------------------------------------

;;-------------------------------------------------------------------
%macro PutLInt 1
push EAX
mov EAX,%1
call proc_PutLInt
pop EAX
%endmacro
;;-------------------------------------------------------------------

;;-------------------------------------------------------------------
%macro GetLInt 1
%ifnidni %1,EAX
push EAX
call proc_GetLInt
mov %1,EAX
pop EAX
%else
call proc_GetLInt
%endif
%endmacro
;;-------------------------------------------------------------------

Binary file added nasm/11/io.o
Binary file not shown.
35 changes: 35 additions & 0 deletions nasm/11/procex1.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
; Parameter passing via registers PROCEX1.ASM
;
; Objective: To show paramenter passing via registers.
; Input: Requests two integers from the user
; Output: Outputs the sum of the input integers.

%include "io.mac"

.DATA
prompt_msg1 db "Please input the first number: ",0
prompt_msg2 db "Please input the second number: ",0
sum_msg db "The sum is ",0
.CODE
.STARTUP
PutStr prompt_msg1 ; request the first number
GetInt CX ; CX = first number
PutStr prompt_msg2 ; request the second number
GetInt DX ; DX = second number
call sum ; returen sum in AX
PutStr sum_msg ; dispaly sum
PutInt AX
nwln
done:
.EXIT

;-------------------------------------------------------------
; Procedure sum recieves two integers in CX and DX.
; Thu sum of the two integers is returned in AX
;-------------------------------------------------------------
sum:
mov AX, CX ; sum = first number
add AX, DX ; sum = sum + second number
ret

44 changes: 44 additions & 0 deletions nasm/11/procex2.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
; Parameter passing via registers PROCEX2.ASM
;
; Objective: To show paramenter passing via registers.
; Input: Requests a character string from the user
; Output: Outputs the length of the input string.

%include "io.mac"
BUF_LEN EQU 41 ; string buffer length
.DATA
prompt_msg db "Please input a string: ",0
length_msg db "The string length is ",0

.UDATA
string resb BUF_LEN ; input string < BUF_LEN chars.
.CODE
.STARTUP
PutStr prompt_msg ; request string input
GetStr string,BUF_LEN ; read string from keyboard

mov EBX,string ; EBX = string address
call str_len ; return string length in AX
PutStr length_msg ; dislpaly string length
PutInt AX
nwln
done:
.EXIT

;-------------------------------------------------------------
; Procedure str_len recieves a pointer to a string in EBX.
; String length is returned in AX
;-------------------------------------------------------------
str_len:
push EBX
sub AX,AX ; string length = 0
repeat:
cmp byte [EBX],0 ; compare with NULL character
je string_length_done ; if NULL , we are done
inc AX ; else, increment the string length
inc EBX ; point EBX to the next char.
jmp repeat ; and repeat the process
string_length_done:
pop EBX
ret

39 changes: 39 additions & 0 deletions nasm/11/procex3.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
; Parameter passing via stack PROCEX3.ASM
;
; Objective: To show paramenter passing via stack.
; Input: Requests two integers from the user
; Output: Outputs the sum of the input integers.

%include "io.mac"

.DATA
prompt_msg1 db "Please input the first number: ",0
prompt_msg2 db "Please input the second number: ",0
sum_msg db "The sum is ",0
.CODE
.STARTUP
PutStr prompt_msg1 ; request the first number
GetInt CX ; CX = first number
PutStr prompt_msg2 ; request the second number
GetInt DX ; DX = second number
push CX ; place first number on stack
push DX ; place second number on stack
call sum ; returen sum in AX
PutStr sum_msg ; dispaly sum
PutInt AX
nwln
done:
.EXIT

;-------------------------------------------------------------
; Procedure sum recieves two integers via the stack
; Thu sum of the two integers is returned in AX
;-------------------------------------------------------------
sum:
enter 0,0 ; save EBP
mov AX, [EBP+10] ; sum = first number
add AX, [EBP+8] ; sum = sum + second number
leave ; restore EBP
ret 4 ; return and clear parameters

Loading

0 comments on commit 18b30b0

Please sign in to comment.