CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2005
    Location
    Romania,Cluj-Napoca
    Posts
    1,073

    Weird problem! In asm.

    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 .read
    end_read:
      mov [si+bx],byte 0 ;null termination 
      inc bx
      mov [si+bx],byte '$';string end
    retn
    
    write:  ;write something must placed in dx->a string with '$'-char at  
     mov ah,0x09
     INT 0x021
    retn
     
    error_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:
    
    retn
    
    WriteInt:  				 ;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_w
    jmp .convert
     .end_w:
     retn
     
    main:
      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

  2. #2
    Join Date
    Apr 2003
    Posts
    1,755

    Smile Re: Weird problem! In asm.

    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
    Code:
    ...
    
    .read:
       mov dx,char_read
       mov ah,3fh
       mov cx,1
       int 0x21
     
       cmp ax,cx
       je .ok
       cmp ax,0
       je .end_read
       cmp ax,cx
       jb error_handling
      
    .ok:  	 
       ;mov si,dx	--- remove this line
       mov dx,[char_read]
    
       call .count
       jmp .read
    .end_read:
    
       ...
    
    section .bss
       file_name resb 100
       handle resb 2
       dots resb 2
       char_read resb 1
    
    ...
    And for outputing word values, look at THIS POST.

    Hope it will help you
    Last edited by rxbagain; December 14th, 2005 at 10:07 AM.

  3. #3
    Join Date
    Mar 2005
    Location
    Romania,Cluj-Napoca
    Posts
    1,073

    Re: Weird problem! In asm.

    Thanks a lot Now works!
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured