Showing posts with label CS401. Show all posts
Showing posts with label CS401. Show all posts

Wednesday, February 17, 2010

CS401

[org 0x0100]
jmp start
filename1: times 128 db 0 ; 1st file name

filename2: times 128 db 0 ; 2nd file name

handle1: dw 0 ; handle for 1st file

handle2: dw 0 ; handle for 2nd file

buffer1: times 1000 db 0 ; buffer for 1st file

errorMSG: db 'file could not be opened$'

success: db 'Successfully copied$'

start: mov ch, 0

mov cl, [0x80] ; command tail length in ex

dec cx ; leave the first space

mov di, 0x82 ; start of command tail in di

mov al, 0x20 ; space for parameter separation

cld ; auto increment mode

repne scasb ; search space

je next ; if found, proceed

jmp problem ; proceed to error printing

next: push cx ; save origional cx

mov si, 0x82 ; set si to start of param

mov cx, di ; set di to end of param

sub cx, 0x82 ; find param size in cx

dec cx ; excluding the space

mov di, filename1 ; set di to space for filename1

rep movsb ; copy filename there

mov byte [di], 0 ; terminate filename with 0

pop cx ; restore original cx

inc si ; go to start of next filename

mov di, filename2 ; set di to space for filename2

rep movsb ; copy filename here

mov byte [di], 0 ; terminate filename with 0

mov ah , 0x3d ; service 3d - open file

mov al, 0 ; read-only mode

mov dx, filename1 ; address of filename

int 0x21 ; DOS service

jnc open ; if no error, proceed for file2

jmp problem ; proceed to error printing

open: mov [handle1], ax ; save handle for first file

push cx ; save cx register

mov cx, 0 ; open file with normal attributes

mov ah, 0x3c ; service 3c - create file

mov dx, filename2 ; address of filename

int 0x21 ; DOS service

pop cx ; restore ex register

jnc store ; if no error, proceed

jmp problem ; proceed to error printing

store: mov [handle2], ax ; save handle for second file

readloop: mov ah, 0x3f ; service 3f - read file

mov bx, [handle1] ; handle for file to read

mov cx, 1000 ; number of bytes to read

mov dx, buffer1 ; buffer to read in

int 0x21 ; DOS service

jnc write ; if no error, proceed

jmp problem ; proceed with error printing

write: test ax, ax ; are zero bytes read?

jz succeeded ; yes, operation completed

mov ah, 0x40 ; else, service 40 - write file

mov bx, [handle2] ; handle for file to read

push cx ;save cx register

mov dx, buffer1 ; buffer to write

int 0x21 ; DOS service

jc problem ; error in writing?

pop cx ; restore cx

cmp ax, cx ; more bytes to read?

je readloop ; yes, start with next byte



succeeded:

mov dx, success ; select success message

jmp exit



problem:

mov dx, errorMSG

exit: mov ah, 9 ; service 9 - output message

int 0x21 ; DOS service

mov ah, 0x3e ; service 3e - close file

mov bx, [handle1] ; handle of file1 to close

int 0x21

mov ah, 0x3e ; service 3e- close file

mov bx, [handle2] ; handle of file2 to close

int 0x21

mov ax, 0x4c00 ; terminate program

int 0x21 ; DOS service

Saturday, January 30, 2010

CS401 GDB

CS401 GDB Solution….
Yes I think that we still need to learn Assembly Language according to following
statements in these days.
1- Assembly language is basic computer knowledge which is very close to
binary instruction. So if anyone wants to get that how your machine works in
a logical way , Assembly Language would be a good choice.
2- As it is a low level language so it also provide fast instructions to processor
and every processor has its own Assembly-Language coding.
3- It provides better understanding of machine codes. that gives you a idea that
how to process your instructions by using low level language.
4- Assembly programming is a must for anyone who wishes to program a micro
controller.
5- Also you can have control over how hardware can respond to electrical timing
of signals as well as manipulation of hardware connected to the CPU.
6- Will you become a better programmer if you do learn assembly?
7- An assembly language is thus specific to a certain physical or virtual computer
architecture (as opposed to most high-level languages, which are usually
portable).
8- An assembly language is a low-level language used in the writing of computer
programs. Assembly language uses mnemonics, abbreviations or words that
make it easier to remember a complex instruction and make programming in
assembler an easier task.

Tuesday, January 19, 2010

CS401

; Searching a substring from a Main string
[org 0x0100]
jmp start

mstr: db 'aaab', 0
sstr: db 'aab', 0
sub_len: dw ; Used to contain length of sub string
start: dw ; Place to resume search
stop: dw ; Last place to begin search

; subroutine to calculate the length of a string
; takes the segment and offset of a string as parameters
strlen: push bp
mov bp,sp
push es
push cx
push di
les di, [bp+4] ; point es:di to string
mov cx, 0xffff ; load maximum number in cx
xor al, al ; load a zero in al
repne scasb ; find zero in the string
mov ax, 0xffff ; load maximum number in ax
sub ax, cx ; find change in cx
dec ax ; exclude null from length
pop di
pop cx
pop es
pop bp
ret 4

; subroutine to compare two strings
; takes segment and offset pairs of two strings to compare
; returns 1 in ax if they match and 0 other wise
strcmp: push bp
mov bp,sp
push cx
push si
push di
push es
push ds

lds si, [bp+4] ; point ds:si to first sstr
les di, [bp+8] ; point es:di to second mstr

push es ; push segment of mstr
push di ; push offset of mstr
call strlen ; calculate string length
mov bx, ax ; save length in bx

push ds ; push segment of sstr
push si ; push offset of sstr
call strlen ; calculate string length
; Now length of sstr is in ax register
; See if mstr/sstr are null or sstr longer than mstr

or bx,bx ; mstr null?
JE exitfalse
cmp ax,0 ; sstr null?
JE exitfalse
cmp ax, bx ; sstr>mstr?
JE exitfalse

mov sub_len, ax ; Move length of sstr in sub_len

; see if the sstr is substring of mstr

LEA SI, sstr ; SI points to sstr
LEA DI, mstr ; DI points to mstr
Cld ; Left to right processing

; Compute STOP

mov stop, DI ; stop has mstr address
add stop, bx
mov cx,sub_len
sub stop,cx ; subtract substring length

; Initialize start

mov start, DI
REPEAT:
; compare characters
mov cx, sub_len
mov DI, start ; reset DI
LEA SI, sstr ; reset SI
Repe cmpsb ; compare characters

JE exitsimple

; substring not found yet

INC start
; see if the start<=stop

mov ax, start
cmp ax, stop
JNLE exitfalse
JMP REPEAT
mov ax,1
JMP exitsimple

exitfalse: mov ax, 0 ; store 0 to mark unequal

exitsimple: pop ds
pop es
pop di
pop si
pop cx
pop bp
ret 8

start: push ds ; push segment of first string
mov ax, mstr
push ax ; push offset of first string
push ds ; push segment of second string
mov ax, sstr
push ax ; push offset of second string
call strcmp ; call strcmp subroutine

mov ax, 0x4c00 ; terminate program
int 0x21