Working with strings from file YASM (8086) - file

I have one task to do. I will try to explain it as clearly as possible.
When you run the program, it asks to input (reading file and writing file).
Reading file has lines in range [1; 999]. Every line has six columns. Every column is separated by semicolon (;).
First and second columns contains text symbols in range [1; 20].
Third - fifth columns contains integers [-100; 100].
Last column contains floating number [-9.99; 9.99]. With two symbols after point.
FILE EXAMPLE:
firstA;lB;lC;lD;lE;lF
A11;bas hello;0;0;5;1.15
B12; good day;-100;11;78;1.33
TASK:
Output: Only first and second columns, that doesn't contain numbers and symbols 'B', 'C'.
OUTPUT:
firstA, because only this column doesn't have 'B', 'C' and numbers.
Up to now I have written program, that just throws away numbers and symbols. I can't figure out the solution for the full TASK.
My program
%include 'yasmmac.inc'
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
org 100h
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
section .text
startas:
macPutString 'Reading file name:', crlf, '$' ;Input for reading file
mov al, 128
mov dx, readingFile
call procGetStr
macNewLine
macPutString 'Writing file name: ', crlf, '$' ;Input for writing file
mov al, 128
mov dx, writingFile
call procGetStr
macNewLine
push readingFile
push writingFile
call function
exit
;Main Function
function:
push bp
mov bp, sp
sub sp, 4
push dx
push bx
push ax
mov dx, [bp+6]
call procFOpenForReading
jnc .secondFOpen
macPutString 'Error while opening file', crlf, '$'
jmp .end
.secondFOpen:
mov [bp-2], bx
mov dx, [bp+4]
call procFCreateOrTruncate
jnc .filter
macPutString 'Error while opening writing file', crlf, '$'
jmp .close
.filter:
mov [bp-4], bx
.whileNotTheEnd:
mov bx, [bp-2]
call procFGetChar
jnc .c1
macPutString 'Error while reading file', crlf, '$'
jmp .close
.c1:
cmp ax, 0 ; Checks if it is not the end of the file
jne .check
jmp .close ; If the end - close the file
.check:
mov al, cl
cmp al, ';' ; Checks if ';'
jne .c2
.c2:
cmp al, 30h ; Number checking
jge .c3
jmp .c4
.c3:
cmp al, 39h ; Number checking
jle .checkEnd
jmp .c4
.c4:
cmp al, 'B'
jne .c5
jmp .checkEnd
.c5:
cmp al, 'C'
jne .writing
jmp .checkEnd
.writing:
mov bx, [bp-4]
call procFPutChar
jnc .checkEnd
macPutString 'Error while writing file', crlf, '$'
jmp .close
.acheckEnd:
cmp ax, 0
jne .nextIteration
jmp .close
.nextIteration:
jmp .whileNotTheEnd
.close:
mov bx, [bp-4]
call procFClose
.closeReadingFile:
mov bx, [bp-2]
call procFClose
.end:
ret4
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
%include 'yasmlib.asm'
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
section .data
writingFile:
times 255 db 00
readingFile:
times 255 db 00
duomenys:
times 255 db 00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
section .bss

There's not much about "yasmmac.inc" to be found online, but I believe that my interpretation of what these procFGetChar and procFPutChar macros do will not be too far off...
The task will seem too complicated if you don't use a modular approach. You need to delegate the simpler subtasks to separate subroutines. Further it would make sense to abstract from where the data is coming from. Don't read a single character from the file and immediately have it classified. No, fetch a complete line from the file and process the requested fields from the memory copy. It will be so much easier and less prone to errors related to file access.
.whileNotTheEnd:
; Fetch one line from the file
call .fgets ; -> DI (AX BX CL)
test di, di
jz .close ; Normal EOF
; Process fields 1 and 2 from the memory copy
mov si, Buffer
call .field ; -> SI (AX BX)
call .field ; -> SI (AX BX)
jmp .whileNotTheEnd
.close:
mov bx, [bp-4]
call procFClose
mov bx, [bp-2]
call procFClose
The lines in the text file each end with a carriage return and linefeed pair (13,10). Finding the 10 therefore denotes the EOL.
Receiving AX=0 from DOS means the end of the file. If this happens when we have not yet characters in our buffer, it signals a normal EOF.
; ----------------------
; IN (bp) OUT (di) MOD (ax,bx,cl)
.fgets:
mov bx, [bp-2] ; Handle
xor di, di ; Counts characters
.more:
call procFGetChar ; -> AX CL CF
jc .err1
test ax, ax ; EOF ?
jz .eof
mov [Buffer + di], cl
inc di
cmp cl, 10
jne .more
.ret:
ret
.eof:
test di, di ; If DI==0 then Normal EOF
jz .ret
.err1:
macPutString 'Error while reading file', crlf, '$'
pop ax ; (*) Forget about `call fgets`
jmp .close
; ----------------------
The field subroutine does the magic of this program but even it will delegate to a further subroutine the classification of the character. This will enhance readability. As a rule of thumb, I try to keep each subroutine within the bounds of one screen (25 rows). Because this code will end with SI pointing after the current field, it will be real easy to continue with the 2nd field.
; ----------------------
; IN (si) OUT (si) MOD (ax,bx)
.field:
mov bx, si ; Remember the start of this field
.check:
lodsb
cmp al, ";"
je .write
call .test ; -> CF
jnc .check
.skip:
lodsb ; Skip remainder of this (bad) field
cmp al, ";"
jne .skip
ret
.write:
push si ; (1)
mov si, bx ; Send this field to file
lodsb
.w1:
call .fputc ; -> (AX BX)
lodsb
cmp al, ";"
jne .w1
pop si ; (1) SI points after the ";"
ret
; ----------------------
; IN (al) OUT (CF)
.test:
cmp al, "0"
jb .OK
cmp al, "9"
jbe .NOK
cmp al, "B"
je .NOK
cmp al, "C"
je .NOK
.OK:
clc
ret
.NOK:
stc
ret
; ----------------------
; IN (al,bp) OUT () MOD (ax,bx)
.fputc:
mov bx, [bp-4] ; Handle
call procFPutChar ; -> AX CF
jc .err2
test ax, ax
jz .err2
ret
.err2:
macPutString 'Error while writing file', crlf, '$'
pop ax ; (*) Forget about `call .fputc`
pop ax ; (*) Forget about `call .field`
jmp .close
; ----------------------
(*) The error exits in .fgets and .fputc need to balance the stack! They need "to forget" about the calls that were made before the jmp to .close can pass in all safety.

Related

How to write decimal number by one char to file 8086 YASM

I have a task and I will try to explain it clearly. There is a file with [0; 1000] lines. Each line contains 6 columns.
The first two columns contain string with [1; 20] characters. Characters could be letters, numbers, and whitespaces.
3-5 columns contain integers in the range [-100; 100].
6th column contain real numbers in range [-9.99; 9.99] with only two digits after decimal point.
Each section I separated by a semicolon ';'.
FILE EXAMPLE:
helloA;lB;lC;lD;lE;lF
A11;bas morning;0;0;5;1.15
B12; Hello WoRlD;-100;11;78;1.33
B11;table;10;0;55;-2.44
C1;OakWood;0;8;17;3.77
TASK: count how many lines in the first two sections contain the letters 'B' and 'C'. And print that integer number in the other file.
I did almost all the task, except one thing. I don't know how to print the decimal number in the file. I store this number in memory as hexadecimal. I need to convert that number to decimal and print it into the other file.
I am struggling because there could be 1 good line, but it also could be 1000 good lines. So I need to print 1 character (if the number of good lines is between [0; 9]), but it could be 900 good lines, so then the program has to print 3 characters.
MY CODE
org 100h
%include 'yasmmac.inc'
section .text
startas:
macPutString 'Output file:', crlf, '$'
; Save the writing file's name
mov al, 128
mov dx, writingFile
call procGetStr
macNewLine
; Open reading file
mov dx, readingFile
call procFOpenForReading
jnc .writingFileOpen
macPutString 'Error while opening the writing file!', '$'
exit
; Open the writing file
.writingFileOpen:
mov [readingDescriptor], bx
mov dx, writingFile
call procFCreateOrTruncate
jnc .writingFileSuccessfullyOpened
macPutString 'Error while opening file for writing!', '$'
jmp .writingError
; Sacing writing descriptor
.writingFileSuccessfullyOpened:
mov [writingDescriptor], bx
; Read first line
call procReadLine
; Main loop
.untilEndOfFile:
call procReadLine
; checking the first two columns
;mov al, ';'
; checking first column
.firstColumn:
mov al, [di]
inc di
cmp al, byte 'B'
je .skipALine
cmp al, byte 'b'
je .skipALine
cmp al, byte 'C'
je .skipALine
cmp al, byte 'c'
je .skipALine
cmp al, byte ';'
jne .firstColumn
; checking second column
.secondColumn:
mov al, [di]
inc di
cmp al, byte 'B'
je .skipALine
cmp al, byte 'b'
je .skipALine
cmp al, byte 'C'
je .skipALine
cmp al, byte 'c'
je .skipALine
cmp al, byte ';'
jne .secondColumn
jmp .addNumber ; Adding number because line corresponds to filter.
.addNumber:
call procAddNumber
; If it is not the end of file, jump back to main loop
.skipALine:
cmp [readTheLastLine], byte 0
je .untilEndOfFile
; Writing to file (number, how many good lines)
; **I cant do this part**
mov bx, [writingDescriptor]
mov cx, 2h
mov dx, lineCount
mov ah, 40h
int 21h
; Closing Files
.end:
mov bx, [writingDescriptor]
call procFClose
.writingError:
mov bx, [readingDescriptor]
call procFClose
exit
%include 'yasmlib.asm'
; void procReadLine()
; Read line to buffer 'line'
procReadLine:
push ax
push bx
push cx
push si
mov bx, [readingDescriptor]
mov si, 0
.loop:
call procFGetChar
; End if the end of file or error
cmp ax, 0
je .endOfFile
jc .endOfFile
; Putting symbol to buffer
mov [line+si], cl
inc si
; Check if there is \n?
cmp cl, 0x0A
je .endOfLine
jmp .loop
.endOfFile:
mov [readTheLastLine], byte 1
.endOfLine:
mov [line+si], byte '$'
mov [lineLength], si
pop si
pop cx
pop bx
pop ax
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
procAddNumber:
push si
push ax
push bx
push cx
push dx
;lineCount++
mov ax, [lineCount]
inc ax
mov [lineCount], ax
pop dx
pop cx
pop bx
pop ax
pop si
ret
section .data
readingFile:
db 'input.dat', 00
readingDescriptor:
dw 0000
writingFile:
times 128 db 00
writingDescriptor:
dw 0000
readTheLastLine:
db 00
line:
db 64
times 66 db '$'
lineLength:
dw 0000
lineCount:
dw 0000
GitHub link to macroses:
yasmlib.asm/yasmmac.inc
Any help would be appreciated.
I don't know how to print the decimal number in the file. I store this number in memory as hexadecimal. I need to convert that number to decimal and print it into the other file.
The solution to the problem is already in the yasmlib.asm file! It contains a code procUInt16ToStr that will convert the unsigned number in AX into an ASCIIZ string at the address in DX.
It does not return the length of the string, so you'll have to loop over the string and use procFPutChar to send the individual characters to the file. Alternatively and preferably loop over the string to establish the stringlength and output all at once with DOS function 40h (like you were doing already).
If you're interested in knowing how to convert from integer to string, then you could read my Q/A Displaying numbers with DOS.
.WritingToFile:
mov dx, Buffer
mov ax, [linecount]
call procUInt16ToStr ; produces an ASCIIZ string at DX
mov si, dx
.again:
lodsb
cmp al, 0
jne .again
sub si, dx
lea cx, [si - 1] ; -1 because we don't want to send the zero
mov bx, [writingDescriptor]
mov ah, 40h ; DOS.WriteToFile
int 21h ; -> AX CF
Watch out with these
.untilEndOfFile:
call procReadLine
.firstColumn:
mov al, [di]
This code is using DI without having initialized the register (mov di, line).
.skipALine:
cmp [readTheLastLine], byte 0
je .untilEndOfFile
Inspecting the readTheLastLine variable comes too late! You need this directly following the return from the procReadLine procedure:
.untilEndOfFile:
call procReadLine
cmp byte [readTheLastLine], 0
je .WritingToFile
mov di, line
.firstColumn:
mov al, [di]
...
jmp .untilEndOfFile
.WritingToFile:
You don't need that wasteful procAddNumber procedure to increment the linecount variable.
Simply replace call procAddNumber by inc word [linecount].
While the other answer dealt with your question about writing the textual representation of the number, this answer focusses on a fundamental misunderstanding about what the task is asking.
TASK: count how many lines in the first two sections contain the letters 'B' and 'C'
Your current program is counting the lines that neither contain a 'B' nor a 'C'. The opposite from what was asked. Next will give you the count of lines that either contain a 'B' or a 'C'.
.untilEndOfFile:
call procReadLine
cmp byte [readTheLastLine], 0
je .WritingToFile
...
jne .secondColumn
jmp .untilEndOfFile
.skipALine:
inc word [linecount]
jmp .untilEndOfFile
.WritingToFile:
Note that the above is still not literally "contain the letters 'B' and 'C'", it's more like "contain the letters 'B' or 'C'". But don't worry, a distinction that is very important in programming might not always be considered that important in day to day speech/writing.

Loop input assembly x86

The program needs to iterate trough the input that a user enters and replace every underscore character ("_") that it finds with "#".
The program does it, but just one time, then every underscore character that is found, stays the same.
.MODEL SMALL
.STACK
.DATA
string_input DB "Enter a string: $"
string_output DB "Your string is: $"
bufferLong db 250
inputLong db 0
buffer db 250, 0, 250 dup ("$"), "$"
.CODE
start:
mov ax, #data
mov ds, ax
mov dx, offset string_input
mov ah, 09h
int 21h
call inputString
;mov cl, al
mov dl, 0Ah
mov ah, 02h
int 21h
mov dl, 0Dh
mov ah, 02h
int 21h
mov dx, offset string_output
mov ah, 09h
int 21h
;mov al, cl
call outputString
mov ax, 4C00h
int 21h
inputString proc
mov dx, offset bufferLong
mov ah, 0Ah
int 21h
ret
inputString endp
outputString proc
mov dx, offset buffer
mov ah, 09h
mov si,0
mov cx, 0
mov bl, "_"
loop:
cmp bl, buffer[si]
je replace
inc cx
inc si
jne loop
replace:
mov buffer[si], "#"
int 21h
ret
outputString endp
end start
bufferLong db 250
inputLong db 0
buffer db 250, 0, 250 dup ("$"), "$"
This looks like you don't truly know what the required input structure for this DOS.BufferedInput function 0Ah should look like. Read more about it at How buffered input works.
This is the correct definition:
bufferLong db 250
inputLong db 0
buffer db 250 dup (0)
Upon the first replacement, you immediately print the result, where in fact you should continu the loop. Better change the conditional jump to the opposite like this (from je to jne):
xor si, si
loop:
mov al, buffer[si]
cmp al, 13
je print ; Exit the loop upon finding 13
cmp al, "_"
jne skip ; While skipping you stay in the loop
replace:
mov buffer[si], "#"
skip:
inc si
jmp loop ; Unconditionally to the top where a possible exit can get detected
print:
mov buffer[si], "$"
mov dx, offset buffer
mov ah, 09h
int 21h
ret
What your 'loop' was missing is a clear way to exit. The string that you process ends with a carriage return (13). Use that to stop iterating. This could happen from the start if the user did not type in characters and just used enter.
Next you need to provide the correct string termination so DOS can print the result. Preloading the input buffer with $ characters serves no purpose. Just store one $ character either replacing the byte 13 or else right behind the byte 13.

Printing an array but specific values highlighted {emu8086}

I want to print this array but also highlight specific characters from said array. This is my code:
include 'emu8086.inc'
.data
animales1 db 'BUBRPQYFODFZXIQ'
db 'MSVDJVQDTLOEATF'
db 'RCZPIFYGAZLPMFN'
db 'LVWKFFBKDXHFIUW'
db 'AOSEFQEMOOTGQUR'
db 'ELLWTGNJJKAJISJ'
db 'OVCOXLUEQTSDDSP'
db 'UEAEMTNYOLVYMGI'
db 'ORREPOMJZGYPHAI'
db 'IFTLCBJFVOYHLUB'
db 'WTOWZQFRAXQRLMR'
db 'KGNYIIHHHKFUKIJ'
db 'XMLSACGMVXEYSIT'
db 'TSOESQVSEQRFNPU'
db 'ODDQMDFWRGETDLY'
lenguajes2 db 'SLKFMBCULKVYUIM'
db 'TWCDQFYIVIKUXKB'
db 'GNIWEQBOSYEMDTJ'
db 'WDHFZZPUIEDERYQ'
db 'KMTGTKAKROMUSUV'
db 'BELBLLTUVJQHCRW'
db 'UPLUBYJKNUXORLF'
db 'SGMAOOEENBOGIWR'
db 'JVSTLPTEGPPNPJW'
db 'YINCASSEMBLYTTU'
db 'CHWTIOIWORDZDRV'
db 'BRZCNRAVRWAMUNU'
db 'KOMCOUKNGQEPQVS'
db 'XXRXJUJUBEXVGGA'
db 'MNKJQKZAACVCLDW'
char db 'a'
flag db 0
;Pez= 12,27,42
;Ave= 76,92,108
indx db 5,11,26,41,45 dup (' ')
.stack
.code
.start up
mov ax,0000h
mov bx,0000h
mov cx,0000h
mov dx,0000h
lea si, animales1
;call valueInArray
call printArrays
printArrays proc
mov bx,0000h
printArray:
mov dx,0000h
mov ah, 02h
mov dl,byte ptr[si+bx]
call valueinArray
cmp flag,1
jz printSpecial
int 21h
mov dl, 20h
int 21h
add bx, 1
add cl, 1
cmp cl,15
jz nextRow
jnz printArray
nextRow:
call nwLine
add ch,1
mov cl, 00h
cmp ch,15
jz finproc
jnz printArray
printSpecial:
lea di,char
mov byte ptr[di],dl
push ax
push bx
push cx
push dx
mov bp, offset char
mov dh,00h
mov al, 1
mov bh, 0
mov bl, 1111_0000b
mov cx, 1; calculate message size.
mov dx,0000h
mov ah, 13h
int 10h
mov dl, 20h
mov ah, 02h
int 21h
pop dx
pop cx
pop bx
pop ax
add bx, 1
add cl, 1
cmp cl,15
jz nextRow
jnz printArray
finproc:
ret
printArrays endp
nwLine PROC
MOV AH,02h
MOV DX,0Ah; imprime asscii 10 (nueva linea)
INT 21h
MOV DX,0Dh; imprime asscii 13 (retorno de carro)
INT 21h
RET
nwLine ENDP
valueInArray proc
mov flag, 0
mov di,0001h
iterar:
mov dh, indx[di]
cmp dh,20h
jz finvalueproc
cmp bl,dh
jz found
add di,1
jmp iterar
found:
mov flag,1
jmp finvalueproc:
finvalueproc:
ret
valueInArray endp
exit:
end
As you can see, the array 'indx' have the positions of the chars (The first number is the effective lenght of said array) that I want to highlight so I check if bx is in the position of said character to print it highlighted.
I have two problems at the moment, the first one is the highlighted characters are printing in a kinda random position and that messes up the rest, but I think I know why the position of the highlighted character that prints is wrong, because the interrupt that I use uses dh and dl as positioners but I do not know how to make it so it continues the flow of the rest. The second problem is that the character that prints is wrong, if you run it and check the variable char, it has an 'Z' but it prints a 'T'. I really need help. Pretty please. :(
indx db 5,11,26,41,45 dup (' ')
It could be that this line is not what you think it is!
Reading your explanation I think you really meant:
indx db 5, 11, 26, 41, 45, ' '
That is the length of the array which is 5, then followed by 4 offsets to the highlighted characters and 1 space character as a list terminator.
the first one is the highlighted characters are printing in a kinda random position and that messes up the rest, but I think I know why the position of the highlighted character that prints is wrong, because the interrupt that I use uses dh and dl as positioners but I do not know how to make it so it continues the flow of the rest.
Because your code to output the highlighted character uses mov dx,0000h, the BIOS function 13h places the character in the upperleft corner of the screen and the other, 'normal' characters will continue from there.
All you have to do is to ask BIOS where the cursor is at that moment using function 03h:
Replace
mov cx, 1; calculate message size.
mov dx,0000h
mov ah, 13h
int 10h
by
mov ah, 03h ; BIOS.GetCursorAndConfiguration
int 10h ; -> CX DX
mov cx, 1 ; message size
mov ah, 13h ; BIOS.WriteString
int 10h
Function 13h fetches from ES:BP. Did you setup the ES segment register?
The second problem is that the character that prints is wrong, if you run it and check the variable char, it has an 'Z' but it prints a 'T'.
Probably resolving the first problem will also resolve the second one.
There's a better, less convoluted way to output your highlighted character.
printSpecial:
push ax
push bx
push cx
mov cx, 1 ; ReplicationCount
mov bx, 00F0h ; DisplayPage 0, Attribute BlackOnBrightWhite (11110000b)
mov al, dl ; Character
mov ah, 09h ; BIOS.WriteCharacterAndAttribute
int 10h
mov ah, 0Eh ; BIOS.TeletypeCharacter
int 10h
pop cx
pop bx
pop ax
...
Function 09h will write your character with the colors that you need, and then function 0Eh will advance the cursor normally.

Define a string array using NASM - Prototype give the number of days in a month

Well as the title said , I am working on a NASM project: the idea is simple , I need to take a month from input (string) and give the number of day (using cases regarding numbers). Until now I spent the day dealing with reading/printing and finally comparing 2 strings , which I could do after some struggles and thanks to some old questions I've found here and on other forums.My current problem is that I need to put the months (names) in an array so I could do the comparing with a loop, I saw on another answer that I could label the 'array' like this:
label: db str1,str2
I tried that and when I try printing with just the label I get only the last month (I tried label+i but I still get the same thing)
Well, here is a part of my code:
segment .data
org 100h
msg db "a"
mon1 db "janvier",0
mon2 db "fevrier",0
mon3 db "mars",0
mon4 db "avril",0
mon5 db "mai",0
mon6 db "juin",0
mon7 db "juillet",0
mon8 db "aout",0
mon9 db "septembre",0
mon10 db "octobre",0
mon11 db "novembre",0
mon12 db "decembre",24h
mo dw 1,2,3,4,5,6,7,8,9,10,11,12
mon:
dw mon1,mon2,mon3,mon4,mon5,mon6,mon7,mon8,mon9,mon10,mon11,mon12
segment .code
mov dx,mon
mov ah,09h
int 21h
edit2: I tried the solution given by #ecm , I had to make a few change because It gave me some errors then after i could finally run it It went to error, Here is the whole code:
segment .code
display_month:
; takes month 1 to 12 in ax
dec ax ; make number 0-based
cmp ax,amount
jae error ; if out of range -->
add ax, ax ; make it an index into a word array
mov bx, ax ; use bx for addressing memory
mov dx, word [mon+ bx] ; access array entry
mov di, dx
mov cx, -1
mov al, 0
repne scasb
not cx
dec cx ; string length
mov bx, 1
mov ah, 40h
int 21h ; write to stdout
clc ; indicate not error
mov dx,msg1
mov ah,09h
int 21h
mov ah,0Ah
mov dx, len ;start of buffer
int 21h
mov ah,02h
mov dl,10
int 21h
mov dl,13
int 21h
mov bx,act
mov dx,buffer
add dl,byte [bx]
mov bx,dx ; move pointer into BX
mov byte [bx],24h ; put the $ there.
; compare input with msg variable(a placeholder for the moment ) I want to compare with the mon array values , and then use the index as an argument to call the cond procedure.
mov ax,msg
mov si,ax
mov ax,buffer
mov di,ax
cmpsb
jz Yep ; if strings equal goto Yep
jmp Nope ; goto Nope
Yep:
mov dx,good
mov ah,9
int 21h ; tell the user it's good
Nope:
mov dx,bad
mov ah,9
int 21h ; tell the user it's bad
end:
mov ah,4Ch
int 21h
ret
error:
stc
retn
cond:
cmp ax,2
je fev
cmp ax,7
jg odd
jle even
else: mov bx,31
jmp endif
fev: mov bx,28
odd: test ax,1
jnz trente
jmp else
even: test ax,1
jp trente
jmp else
trente: mov bx,30
endif: ret
segment .data
org 100h
;; variables declaration
msg1 db "Veullez entrer un mois:",24h
msg db "a",24h
mon1: db "janvier",0
mon2: db "fevrier",0
mon3: db "mars",0
mon4: db "avril",0
mon5: db "mai",0
mon6: db "juin",0
mon7: db "juillet",0
mon8: db "aout",0
mon9: db "septembre",0
mon10: db "octobre",0
mon11: db "novembre",0
mon12: db "decembre",0
good db "Bon choix!",24h
bad db "Mauvais choix!",24h
mon:
start:
dw mon1,mon2,mon3,mon4,mon5,mon6,mon7,mon8,mon9,mon10,mon11,mon12
tend:
len db 254 ; a fair amount of space
act db 0 ; will be filled with actual amount of chars received
buffer times 254 db 0
size equ tend - start
amount equ size / 2
I forgot to add how I am compiling , well I am using dosbox to run .com files , because they don't work on windows 10 , and I use the command : nasm name.asm -o name.com to create the .com file and then I just open it in dos.
EDIT: Well I struggled a lot to make that happen , I couldn't do it even thought I tried different ways , the last thing I went with was to use the first month and keeping adding the size of months to pass to the other ones (i.e mon1+8 gives me mon2 ...) but then I encountered with the difference in sizes , so I changed all the months to just 3 letters (4 for june) so I could move with multiples of 4 but then I couldn't go all the way to the end .. So after that I just decided to use the mon(i) by calling the names and repeating the manoeuvre , which seemed to work thought I encountered a problem with comparing (I am still trying to figure out) , well here is the last version until I figure out how to fix that.The problem at this moment is that even if I type something different than the mon1 to mon12 I get this:Le mois de 'input' est de 30 jours while normally it should take me back to the start of the nope label.
segment .code
org 100h
mov dx,msg1
mov ah,09h
int 21h
here: mov ah,0Ah
mov dx, len
int 21h
mov ah,02h
mov dl,10
int 21h
mov dl,13
int 21h
mov bx,act
mov dx,buffer
add dl,byte [bx]
mov bx,dx
mov byte [bx],24h
jmp comp
yep:
mov dx,g1
mov ah,09h
int 21h
mov dx,buffer
int 21h
mov dx,g2
int 21h
mov ax, [i]
call cond
mov ah,02h
mov dl,bh
int 21h
mov dl,bl
int 21h
mov dx,g3
mov ah,09h
int 21h
jmp end
comp: mov bx,0
mov [i],bx
jan: call inct
mov bx,buffer
mov si,bx
mov bx,mon1
mov di,bx
cmpsb
jz yep
fevr: call inct
mov bx,buffer
mov si,bx
mov bx,mon2
mov di,bx
cmpsb
jz yep
mar: call inct
mov bx,buffer
mov si,bx
mov bx,mon3
mov di,bx
cmpsb
jz yep
avr: call inct
mov bx,buffer
mov si,bx
mov bx,mon4
mov di,bx
cmpsb
jz yep
mai: call inct
mov bx,buffer
mov si,bx
mov cx,mon5
mov di,cx
cmpsb
jz yep
juin: call inct
mov bx,buffer
mov si,bx
mov bx,mon6
mov di,bx
cmpsb
jz yep
jmp jui
jui: call inct
mov cx,buffer
mov si,cx
mov cx,mon7
mov di,cx
cmpsb
jz yep
aout: call inct
mov cx,buffer
mov si,cx
mov cx,mon8
mov di,cx
cmpsb
jz yep
jmp sep
sep: call inct
mov bx,buffer
mov si,bx
mov cx,mon9
mov di,cx
cmpsb
jz yep
jmp oct
oct: call inct
mov bx,buffer
mov si,bx
mov cx,mon10
mov di,cx
cmpsb
jz yep
jmp nov
nov: call inct
mov bx,buffer
mov si,bx
mov cx,mon11
mov di,cx
cmpsb
jz yep
jmp dect
dect: call inct
mov bx,buffer
mov si,bx
mov cx,mon12
mov di,cx
cmpsb
jz yep
jmp nope
nope:
mov dx,bad
mov ah,9
int 21h
jmp here
end:
mov ah,4Ch
int 21h
ret
inct:
push bx
mov bx,[i]
inc bx
mov [i],bx
pop bx
ret
cond:
cmp ax,2
je fev
cmp ax,6
je th
cmp ax,8
je th
cmp ax,7
jg odd
jle even
else: mov bl,'1'
mov bh,'3'
jmp endif
fev: mov bl, '8'
mov bh,'2'
jmp endif
th: jmp else
odd: test ax,1
jnz trente
jmp else
even: test ax,1
jp trente
jmp else
trente: mov bh,'3'
mov bl,'0'
endif: ret
segment .data
i db 0
msg1 db "Veullez entrer un mois:",24h
mon1: db "janvier",24h
mon2: db "fevrier",24h
mon3: db "mars",24h
mon4: db "avril",24h
mon5: db "mai",24h
mon6: db "juin",24h
mon7: db "juillet",24h
mon8: db "aout",24h
mon9: db "septembre",24h
mon10: db "octobre",24h
mon11: db "novembre",24h
mon12: db "decembre",24h
g1 db "Le mois de",20h,24h
g2 db 20h,"est de",20h,24h
g3 db 20h,"jours",24h
bad db "Le mois saisi n'est pas correct!",10,13,"Veuillez entrer un autre mois:",24h
len db 254 ; a fair amount of space
act db 0 ; will be filled with actual amount of chars received
buffer times 254 db 0
Try this:
segment .code
display_month:
; takes month 1 to 12 in ax
dec ax ; make number 0-based
cmp ax, montab.amount
jae .error ; if out of range -->
add ax, ax ; make it an index into a word array
mov bx, ax ; use bx for addressing memory
mov dx, word [montab + bx] ; access array entry
mov di, dx
mov cx, -1
mov al, 0
repne scasb
not cx
dec cx ; string length
mov bx, 1
mov ah, 40h
int 21h ; write to stdout
clc ; indicate not error
retn
.error:
stc
retn
segment .data
mon1: db "janvier",0
mon2: db "fevrier",0
mon3: db "mars",0
mon4: db "avril",0
mon5: db "mai",0
mon6: db "juin",0
mon7: db "juillet",0
mon8: db "aout",0
mon9: db "septembre",0
mon10: db "octobre",0
mon11: db "novembre",0
mon12: db "decembre",0
align 2
montab:
.:
dw mon1
dw mon2
dw mon3
dw mon4
dw mon5
dw mon6
dw mon7
dw mon8
dw mon9
dw mon10
dw mon11
dw mon12
.end:
.size equ .end - .
.amount equ .size / 2
You could use 12 as the hardcoded array length in this case. But using equates for the length and amount of entries is useful for static data arrays more generally.
ETA: I added alignment for the table, which is good for performance. Not needed but it doesn't cost much.
Note that I dropped the org directive from my example. This is because I am presenting just one function that should be called from other program logic. If you're assembling into a simple style flat .COM executable for 86-DOS, you still need to include the org 256 at some point.
Here's a breakdown of your question's attempt:
msg db "a"
This seems like an unused leftover.
mon1 db "janvier",0
mon2 db "fevrier",0
mon3 db "mars",0
mon4 db "avril",0
mon5 db "mai",0
mon6 db "juin",0
mon7 db "juillet",0
mon8 db "aout",0
mon9 db "septembre",0
mon10 db "octobre",0
mon11 db "novembre",0
These are all fine, they define ASCIZ strings. (The Z stands for zero-terminated.)
mon12 db "decembre",24h
This one uses a different terminator, in this case the dollar sign (24h = 36). It is not appropriate to mix terminators between array entries. Either use all ASCIZ or all CP/M-style dollar-terminated strings.
mo dw 1,2,3,4,5,6,7,8,9,10,11,12
This is useless. If you wanted to map a number from 1 to 12 as index into this array, the original value would be found at that array entry. If the entries had different numbers this type of array could be useful, but not with identity mapping.
mon:
dw mon1,mon2,mon3,mon4,mon5,mon6,mon7,mon8,mon9,mon10,mon11,mon12
This is essentially correct. (I put each entry on its own line and prepended the align directive, but neither is absolutely necessary.)
mov dx,mon
mov ah,09h
int 21h
This loads the address of your message table into dx then passes dx to interrupt 21h function 09h. This function expects a dollar-terminated string, so only your December string would work. More importantly, it will try to display the literal bytes that make up your array. You need to, instead, dereference a pointer to one of your array's entries to load the address stored therein, which is the address of the associated message string.

TASM: Generating a simple calculator

i am trying to make a simple calculator in TASM, its not done yet, i am trying to figure out addition part but i am stuck because of Operands type do not match, here is what i have;
;FILENAME: SimpleClc.asm
;FILE FORMAT: EXE
PAGE 55,132
.386
STACK_SEG SEGMENT STACK USE16
DB 100 DUP(?)
STACK_SEG ENDS
DATA_SEG SEGMENT 'DATA' USE16
ADDITION MACRO Result,Char1,Char2
MOV AX,Char1
CWD
ADD AX,Char2,
MOV Result,AX
ENDM
SUBTRACTION MACRO Char1, Char2
SUB Char1,Char2
ENDM
DIVISION MACRO Char1, Char2
DIV Char1,Char2
ENDM
MULTIPLICATION MACRO Char1, Char2
MUL Char1,Char2
ENDM
Mainmsg DB 'Please enter a algebraic command line: $'
ErMessage DB 'Error!!', 0DH, 0AH
DB 'INPUT FORMAT:Operand1 Operator Operand',0DH, 0AH
DB 'Operand: Decimal Numbers',0DH, 0AH
DB 'Operator: + -'
INCHAR DB 21
Res DB 10 dup('$')
INCDAT DB 21 dup('$')
VarX DB 10 dup('$')
VarY DB 10 dup('$')
DATA_SEG ENDS
CODE_SEG SEGMENT PARA 'CODE' PUBLIC USE16
ASSUME CS:CODE_SEG, DS:DATA_SEG, SS:STACK_SEG
MAIN PROC FAR
PUSH DS ;INITIATE THE PROGRAM
XOR AX,AX
PUSH AX
MOV AX,DATA_SEG
MOV DS,AX
AGAIN:
LEA DX,Mainmsg ;PRINT MESSAGE
MOV AH,9
INT 21H
MOV DX, OFFSET INCHAR
MOV AH,0AH
INT 21H
MOV DX, OFFSET INCHAR
MOV AH,0AH
INT 21H
LEA DX,INCDAT ;Writing the incoming input
MOV AH,9
INT 21H
MOV DI,OFFSET INCDAT
MOV AL, [DI]
CMP AL, 9
JLE OPERAND ; jump if less or equal
JG ERRORMESSAGE ; jump if not less or equal
INC DI
OPERAND:
MOV AL, [DI]
CMP AL, '+'
JE LASTOPERAND
CMP AL, '-'
JE LASTOPERAND
CMP AL, '*'
JE LASTOPERAND
CMP AL, '/'
JE LASTOPERAND
JMP ERRORMESSAGE
INC DI
LASTOPERAND:
MOV AL, [DI]
CMP AL, 9
JLE OPERATION ; jump if less or equal
JG ERRORMESSAGE ; jump if not less or equal
OPERATION:
CMP AL, '+'
JE ADDITION1
JMP AGAIN
ADDITION1:
MOV DI,OFFSET INCDAT
MOV AL,[DI]
MOV VarX,AL
MOV AL,[DI+2]
MOV VarY,AL
ADDITION Res,VarX,VarY
JMP AGAIN
CMP AL, '-'
JE SUBTRACTION1
JMP AGAIN
SUBTRACTION1:
CMP AL, '*'
JE MULTIPLICATION1
JMP AGAIN
MULTIPLICATION1:
JMP AGAIN
CMP AL, '/'
JE DIVISION1
JMP AGAIN
DIVISION1:
ERRORMESSAGE:
LEA DX,ErMessage ;PRINT MESSAGE
MOV AH,9
INT 21H
MAIN ENDP
CODE_SEG ENDS
END MAIN
I know it looks quite messy right now, it is just because i am trying bunch of things at the same time, btw my calculator will not calculate results that are greater than 10. Thanks for help.Any comment will be appreciated.
Ups, it is hard to digit all the linenumbers.
To place code inside the data segment is not the best idea.
For what this following instruction are good?:
PUSH DS ; pushing the old address of DS to the stack
XOR AX,AX
PUSH AX ; pushing a zero word to the stack
And at last how to terminate the programm?
I am not sure, but maybe the errors occur for using labelnames that starts similar like the names of assembler mnemonics.
And why using the input function one after another for to override the same buffer?
MOV DX, OFFSET INCHAR
MOV AH,0AH
INT 21H
MOV DX, OFFSET INCHAR
MOV AH,0AH
INT 21H

Resources