Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

HELP ME PLEASE!!! it is a project i have to do...

i am a complete newbie when it comes to assembly so please dont scream at me..

the objective of my project is to read from any text file copy everything and convert them to binary THEN covert that binary back to its original text and output that text into a new output file..

so this is my code

TITLE conversion and output to a file

;-----------------------------------------------------------------

;program description: read from a file then convert char to binary
;then convert back to text into output file
;author: Yao Jiang
;creation date: 12-01-09

;-----------------------------------------------------------------

include /masm32/include/masm32rt.inc

;-----------------------------------------------------------------

.data

inname db 'file1.txt',0
outname db 'file2.dat',0
inh dw ?
outh dw ?
buff db ?

;-----------------------------------------------------------------

.code

openfile:

mov dx, offset inname
mov ah, 3dh
mov al, 0 ;0 to open the file to read
int 21h
jc error ;check for error, ie file not found
;or cant be opened
mov inh, ax ;save file handle

read:

mov ah, 3fh ;read data from file
mov bx, inh
mov dx, offset buff
mov cx, 1 ;read 1 type
int 21h
jc error
cmp ax, cx ;check for end of file
jne endoffile
mov al, buff
call write
jmp read

endoffile:

mov ah, 3eh ;close file
mov bx, inh
int 21h
mov ah, 3eh
mov bx, outh
int 21h

PROC write

push ax bx cx dx
mov ah, 2
mov dl, buff
int 21h
pop ax bx cx dx
ret

ENDP

end start

;-----------------------------------------------------------------

so far nothing works, for some reason i cant open the file and im having trouble trying to do conversion before writing to the new file

PLEASE HELP!!!

by the way im using masm32 because professor told the class to

Comments

  • yaochen42yaochen42 Posts: 4Member
    anyone??? please help!!!
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]Segment register(s) not set - see RED...[/color]
    :
    : TITLE conversion and output to a file
    :
    : ;-----------------------------------------------------------------
    :
    : ;program description: read from a file then convert char to binary
    : ;then convert back to text into output file
    : ;author: Yao Jiang
    : ;creation date: 12-01-09
    :
    : ;-----------------------------------------------------------------
    :
    : include /masm32/include/masm32rt.inc
    :
    : ;-----------------------------------------------------------------
    :
    : .data
    :
    : inname db 'file1.txt',0
    : outname db 'file2.dat',0
    : inh dw ?
    : outh dw ?
    : buff db ?
    :
    : ;-----------------------------------------------------------------
    :
    : .code

    [color=Red]: mov ax, @DATA
    : mov ds, ax[/color]
    :
    : openfile:
    :
    : mov dx, offset inname
    : mov ah, 3dh
    : mov al, 0 ;0 to open the file to read
    : int 21h
    : jc error ;check for error, ie file not found
    : ;or cant be opened
    : mov inh, ax ;save file handle
    :
    : read:
    :
    : mov ah, 3fh ;read data from file
    : mov bx, inh
    : mov dx, offset buff
    : mov cx, 1 ;read 1 type
    : int 21h
    : jc error
    : cmp ax, cx ;check for end of file
    : jne endoffile
    : mov al, buff
    : call write
    : jmp read
    :
    : endoffile:
    :
    : mov ah, 3eh ;close file
    : mov bx, inh
    : int 21h
    : mov ah, 3eh
    : mov bx, outh
    : int 21h
    :
    : PROC write
    :
    : push ax bx cx dx
    : mov ah, 2
    : mov dl, buff
    : int 21h
    : pop ax bx cx dx
    : ret
    :
    : ENDP
    :
    : end start
    :
    : ;-----------------------------------------------------------------
    :
    : so far nothing works, for some reason i cant open the file and im
    : having trouble trying to do conversion before writing to the new file
    :
    : PLEASE HELP!!!
    :
    : by the way im using masm32 because professor told the class to
    :
    :
  • yaochen42yaochen42 Posts: 4Member
    i just do the same thing and its still not working... still giving the error "cannot open file"
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]I do not have masm32 - I use FASM, so I can't really run the code and see. Do you have a debugger? If not, I suggest AFD - it is an OK debugger for DOS code. You can get it here:

    [link=http://www.programmersheaven.com/download/21643/download.aspx]http://www.programmersheaven.com/download/21643/download.aspx[/link]
    [/color]
  • anthrax11anthrax11 Posts: 511Member
    I haven't tested it, but it seems that:

    1) the output file isn't opened, only the input file is, the code for it should look like this:
    [code]
    mov dx, offset outname
    mov ah, 3dh
    mov al, 1 ;1 to open the file to write
    int 21h
    jc error ;check for error, ie file not found
    ;or cant be opened
    mov outh, ax ;save file handle
    [/code]
    2) your error handler is missing (I assume you just left it out to save space since you're also missing the "start:" label)
    3) the exit command is missing, add this after closing the files:
    [code]
    mov ax, 4c00h
    int 21h
    [/code]
    4) registers must be popped back in reverse order
    5) your writing to file code is missing, try this:
    [code]
    PROC write

    push ax bx cx dx
    mov ah, 2
    mov dl, buff
    int 21h
    [color=Red]
    mov ah, 40h
    mov bx, outh
    mov cx, 1
    mov dx, offset buff
    int 21h

    pop dx cx bx ax
    [/color]
    ret

    ENDP
    [/code]
  • yaochen42yaochen42 Posts: 4Member
    for the line

    mov ax, @data

    i keep getting the error "undefined symbol"
  • anthrax11anthrax11 Posts: 511Member
    Try one of these:
    [code]
    mov ax, data
    mov ax, seg inname
    [/code]
    Also, consider that the strength of Masm32 is 32-bit Windows programming. For 16-bit, [link=http://flatassembler.net/]Fasm[/link] is more suitable.
Sign In or Register to comment.