simple linux assembly program
July 20, 2014
assembly
There are three simple linux assembly program I wrote today.
##eatcall
; This is a "Hello-world" program
; makefile is:
;
;eatsyscall: eatsyscall.o
; ld -o eatsyscall eatsyscall.o
;eatsyscall.o: eatsyscall.asm
; nasm -f elf -g -F stabs eatsyscall.asm
SECTION .data
EatMsg: db "Eat at Liu's", 10
EatLen: equ $-EatMsg
SECTION .bss
SECTION .text
global _start
_start:
nop
mov eax, 4 ; system write
mov ebx, 1 ; standard ouput
mov ecx, EatMsg
mov edx, EatLen
int 80h
mov eax, 1
mov ebx, 0
int 80h
; The Following is the result:
;~/Hack/SedAwk/asm/eatsyscall->./eatsyscall
;Eat at Liu's
##Lower2Upper
; This program turns lowercase character to uppercase
; character, read one character from file each time
; makefile is:
;
;Lower2Upper: Lower2Upper.o
; ld -o Lower2Upper Lower2Upper.o
;Lower2Upper.o: Lower2Upper.asm
; nasm -f elf -g -F stabs Lower2Upper.asm
SECTION .bss
Buff resb 1
SECTION .data
SECTION .text
global _start
_start:
nop
Read:
mov eax, 3 ; system read
mov ebx, 0 ; standard input
mov ecx, Buff
mov edx, 1
int 80h
cmp eax, 0
je Exit ; EOF, file exhausted
; do char transfer
cmp byte [Buff], 61h ; less than character 'a'
jb Write
cmp byte [Buff], 7ah ; greater than character 'z'
ja Write
; the current character is a lowercase character
; we can transfer it to uppercase
sub byte [Buff], 20h
Write:
mov eax, 4 ; system Write
mov ebx, 1 ; standard output
mov ecx, Buff
mov edx, 1
int 80h
jmp Read ; read next character
Exit:
mov eax, 1
mov ebx, 0
int 80h
; The Following is the result:
;~/Hack/SedAwk/asm/Lower2Upper->cat in.txt
;Hello, I am liuxueyang.
;Are you sad?
;~/Hack/SedAwk/asm/Lower2Upper->./Lower2Upper < in.txt > out.txt
;~/Hack/SedAwk/asm/Lower2Upper->cat out.txt
;HELLO, I AM LIUXUEYANG.
;ARE YOU SAD?
##Lower2Upper_pro
; This program does the same thing as Lower2Upper.asm
; The only progress is that it reads a buffer of characters
; each time.
; makefile is:
;
;Lower2Upper_pro: Lower2Upper_pro.o
; ld -o Lower2Upper_pro Lower2Upper_pro.o
;Lower2Upper_pro.o: Lower2Upper_pro.asm
; nasm -f elf -g -F stabs Lower2Upper_pro.asm
SECTION .bss
BUFFLEN: equ 1024 ; in fact the colon here can be omitted ;-)
Buff: resb BUFFLEN
SECTION .data
SECTION .text
global _start
_start:
nop
read:
mov eax, 3 ; system read
mov ebx, 0 ; standard input
mov ecx, Buff
mov edx, BUFFLEN
int 80h
mov esi, eax
cmp eax, 0
je Exit
mov ecx, esi
mov ebp, Buff ; Buff addr
dec ebp ; on pos before Buff, in order to use ecx properly
Scan:
; transfer chars
cmp byte [ebp+ecx], 61h
jb Next
cmp byte [ebp+ecx], 7ah
ja Next
; the current characters is a lowercase character
sub byte [ebp+ecx], 20h
Next:
dec ecx
jnz Scan ; process former character in Buff
; Buff exhausted, it is time to write the Buffer to output
Write:
mov eax, 4
mov ebx, 1
mov ecx, Buff
mov edx, esi
int 80h
jmp read ; get next Buff
Exit:
mov eax, 1
mov ebx, 0
int 80h
; The Following is the running result:
;~/Hack/SedAwk/asm/Lower2Upper_pro->cat in.txt
;Hello, I am liuxueyang.
;Are you sad?
;~/Hack/SedAwk/asm/Lower2Upper_pro->./Lower2Upper_pro < in.txt > out.txt
;~/Hack/SedAwk/asm/Lower2Upper_pro->cat out.txt
;HELLO, I AM LIUXUEYANG.
;ARE YOU SAD?