Hi. This is another homwork
I have to do in Newtwide Assembler (nasm) to open a file and count the dots "." in that file and write in the output(on the screen; the number of dots)
So I writed that but it doesn't work... gives me a weird output....
I tried to debug it in TD.exe(Turbo debugger(pascal)) and works fine(the output is what I expect). But if I run the exe file it gives me a wrong output. So what coud be weong??? Manny thanks
Code:
ection .code
..start:
mov ax, data
mov ds, ax ; data and stack
mov ax, stack
mov ss, ax
mov sp, stacktop
jmp main
ReadString: ;read a string
xor bx,bx
.read:
mov ah,0x01
INT 0x021
cmp al,0Dh ;if enter end reading
je end_read
mov [si+bx],al
inc bx
jmp .readend_read:
mov [si+bx],byte 0 ;null termination
inc bx
mov [si+bx],byte '$';string end
retnwrite: ;write something must placed in dx->a string with '$'-char at
mov ah,0x09
INT 0x021
retnerror_handling:
push ax
mov dx,msg_error
call write
xor ax,ax
INT 0x016
pop ax
mov ax, 0x4C00 ;end file was an error
int 0x21
ReadChar: ;read the caracter from the file
open_file:
mov ah,3dh ;open file
mov al,0
mov dx,file_name
int 0x021
jc error_handling ;if problems then error
mov [handle],ax
mov bx,[handle] ;file handler
.read:
xor dx,dx
mov ah,3fh ;file reading
mov cx,1 ;we read one byte
int 0x21
cmp ax,cx ;if ax=cx then read ok
je .ok
cmp ax,0 ;if ax=0 then end reading
je .end_read
cmp ax,cx ;if ax<cx then error
jb error_handling.ok:
mov si,dx
mov dx,[si] ;we put in dx the data(the caracter)
call .count ;we count the dots(calling the ".count procedure"
jmp .read.end_read:
mov ah,3eH ;close the file
mov bx,[handle]
int 0x21
retn.count:
cmp dl,'.'
je .dot jmp .count_end.dot:
mov ax,[dots]
add ax,1
mov [dots],ax
jmp .count_end
.count_end:
retnWriteInt: ;write out an integer
mov cx,10
mov ax,bx
.convert:
xor dx,dx
div cx
push ax
mov ah,0x02
add dl,48 ;write's out the number
INT 0x21
pop ax
cmp ax,0
je .end_wjmp .convert.end_w: retnmain:
mov si,file_name
call ReadString
mov dx,file_name
call ReadChar
mov dx,_dots
call write
mov bx,[dots]
call WriteInt
mov ax, 0x4C00 ; end program
int 0x021
section .data ; data segment
msg_error db "An error has occured$"
_dots db "dots number:$"
resb 2
section .bss
file_name resb 100
handle resb 2
dots resb 2
section .stack stack ; stack segment (4 KB)
space resb 4096
stacktop:
Please use code tags [code] [/code]
We would change the world, but God won't give us the sourcecode..
Undocumented futures are fun and useful....
_________
Gili
With my testing on your code, The bug appears to be at the time reading the a character from the file. You set the destination (DX) at 0 (DS:0) which overwrites some of the code sections (in my testing).
You should allocate a variable to place the character read, not just setting it to 0
Bookmarks