2 Replies - 2753 Views - Last Post: 10 December 2012 - 02:36 AM

#1 Muphet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-May 12

Write data into text file

Posted 02 December 2012 - 01:07 PM

Hi. I have to write simple program in assembly (using tasm for compiling) which will add to numbers and save their sum into text file. I have managed to do first part, but when trying to save sum into file it won't work (operand doesn't match). Dunno what i'm doing wrong. Can someone help me or rewrite code to show me, how it should be done?
.model small
.stack 200H
.data
   msg1 DB 10,13,'Enter the first no: $'
   msg2 DB 10,13,'Enter the second no: $'
   msg3 DB 10,13,'sum = $'
   newline DB 10,13, ' $'
   file DB "C:\tasm\results.txt",00
.code
   print MACRO msg ;macro definition
   PUSH AX
   PUSH DX
   MOV AH,09H
   MOV DX,offset msg
   INT 21H
   POP DX
   POP AX
   ENDM
.startup
   print msg1
   CALL readnumtoAX
   MOV CX,AX
   print newline
   print msg2
   CALL readnumtoAX
   print newline
   print msg3
   ADD ax,cX
   CALL displayAX
   CALL saveAX
.exit
readnumtoAX PROC NEAR
   PUSH BX
   PUSH CX
   MOV CX,10
   MOV BX,00
back:
   MOV AH,01H
   INT 21H
   CMP AL,'0'
   JB skip
   CMP AL,'9'
   JA skip
   SUB AL,'0'
   PUSH AX
   MOV AX,BX
   MUL CX
   MOV BX,AX
   POP AX
   MOV AH,00
   ADD BX,AX
   JMP back
skip:
   MOV AX,BX
   POP CX
   POP BX
   RET
readnumtoAX ENDP
displayAX PROC NEAR
   PUSH DX
   PUSH CX
   PUSH BX
   PUSH AX
   MOV CX,0
   MOV BX,10
back1:
   MOV DX,0
   DIV BX
   PUSH DX
   INC CX
   OR AX,AX
   JNZ back1
back2:
   POP DX
   ADD DL,30H
   MOV AH,02H
   INT 21H
   LOOP back2
   POP AX
   POP BX
   POP CX
   POP DX
   RET
  displayAX ENDP
  saveAX PROC
	MOV AH,3D
	MOV AL,20
	MOV DX,file
	INT 21h
	MOV BX,AX
	MOV AX,4000
	MOV DX,cX
	MOV CX,0D
	INT 21H
	MOV AX,4c00
	INT 21H
	saveAX ENDP
end


Is This A Good Question/Topic? 0
  • +

Replies To: Write data into text file

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 866
  • View blog
  • Posts: 2,309
  • Joined: 28-March 11

Re: Write data into text file

Posted 02 December 2012 - 01:42 PM

This is what I get:

Quote

D:\TASM\BIN>tasm32 test2.asm
Turbo Assembler Version 5.0 Copyright © 1988, 1996 Borland International

Assembling file: test2.asm
**Error** test2.asm(88) Operand types do not match
**Error** test2.asm(95) Illegal number
Error messages: 2
Warning messages: None
Passes: 1


Let's take a look at line 88:
	MOV DX,file

That is not correct! You want to load the address of the file name. You have 2 ways of doing this:
use LoadEffectiveAddress - LEA or use the OFFSET keyword.

Next, lets look at line 95:
	MOV AX,4c00

Well, TASM is telling you that is not a valid number... That is not a valid HEX number in any Assembler I know. What is the proper format of a hex number?

TASM is old and decrepit. I had to set up my environment just to get it to run. On top of that, you could of just posted the errors that TASM gave you instead of being cryptic by saying

Quote

operand doesn't match


When you have problems, it is EXTREMELY helpful to post the exact error messages including line numbers!

If you had posted this:

Quote

Assembling file: test2.asm
**Error** test2.asm(88) Operand types do not match
**Error** test2.asm(95) Illegal number
Error messages: 2
Warning messages: None
Passes: 1

I could of just looked at your post and told you what was wrong.
Was This Post Helpful? 0
  • +
  • -

#3 Muphet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-May 12

Re: Write data into text file

Posted 10 December 2012 - 02:36 AM

So for now, program works exactly as I wanted but theres is small thing that i want to know. Writing bits to the file. Dunno why it's required but it actually is. I found, that writing data to file is moving byte number to CX register. It's working for 1 byte, but when I try to write more (for example same two digit number), it saves only one number and one ascii code. This works with every next byte. Problem is also in writing. It causes to save only the last character instead from left to right.
This is my code, writing to file is located at the end.
.model small
.stack 200H
.data
   msg1 DB 10,13,'Enter the first no: $'
   msg2 DB 10,13,'Enter the second no: $'
   msg3 DB 10,13,'sum = $'
   newline DB 10,13, ' $'
   file DB 'results.txt',00
   sum DW ?
.code
   print MACRO msg ;macro definition
   PUSH AX
   PUSH DX
   MOV AH,09H
   MOV DX,offset msg
   INT 21H
   POP DX
   POP AX
   ENDM
.startup
   print msg1
   CALL readnumtoAX
   MOV CX,AX
   print newline
   print msg2
   CALL readnumtoAX
   print newline
   print msg3
   ADD ax,cX
   CALL displayAX
   CALL saveAX
.exit
readnumtoAX PROC NEAR
   PUSH BX
   PUSH CX
   MOV CX,10
   MOV BX,00
back:
   MOV AH,01H
   INT 21H
   CMP AL,'0'
   JB skip
   CMP AL,'9'
   JA skip
   SUB AL,'0'
   PUSH AX
   MOV AX,BX
   MUL CX
   MOV BX,AX
   POP AX
   MOV AH,00
   ADD BX,AX
   JMP back
skip:
   MOV AX,BX
   POP CX
   POP BX
   RET
readnumtoAX ENDP
displayAX PROC NEAR
   PUSH DX
   PUSH CX
   PUSH BX
   PUSH AX
   MOV CX,0
   MOV BX,10
back1:
   MOV DX,0
   DIV BX
   PUSH DX
   INC CX
   OR AX,AX
   JNZ back1
back2:
   POP DX
   ADD DL,30H
   MOV AH,02H
   INT 21H
   	MOV sum,ax
   LOOP back2
   POP AX
   POP BX
   POP CX
   POP DX
	RET
  displayAX ENDP
  saveAX PROC NEAR
	MOV AH,3CH
	MOV CX,0
	LEA DX,file
	INT 21H
	MOV BX,AX
	MOV AH,40H
	MOV DX,offset sum
	MOV CX,1
	INT 21H
	MOV AH,04CH
	INT 21H
  saveAX ENDP
end


How to write more bytes to file which depends on addition result?
(for those who don't know what's going on, this is simple program to sum two user input digits and then save result to txt file)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1