-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
410 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
Oops, something went wrong.