I'm trying to create a proc in TASM that accepts arrays as parameters. Problem is, the result is all messed up. This is the code:
TextParameter equ [bp+8]
MenuColorParameter equ [bp+6]
RowToPrint equ [bp+4]
PrintBar proc
push bp
mov bp, sp
mov dh, RowToPrint
mov dl, 0
mov bh, 0
mov ah, 2
int 10h
mov si, 0
mov cx, 1
##Print:
mov ah, 9
mov al, [TextParameter+si]
mov bl, [MenuColorParameter+si]
int 10h
mov ah, 2
inc dl
int 10h
inc si
cmp si, MENU_LEN ; const in DATASEG
jc ##Print
mov ah, 2
mov bh, 0
mov dl, 0
mov dh, 2
int 10h
pop bp
ret 6
PrintBar endp
When referencing the array through the DATASEG (as in mov al, Array[si]), the proc works as expected.
I'm assuming that you're passing the arrays by-address (i.e. their offsets), since the arguments seem to be 2 bytes each. If so, you need to first load that address into a register, and then add si and do another read from memory:
mov bx, TextParameter ; expands to mov bx,[bp+8]
mov al, [bx+si]
mov bx, MenuColorParameter ; expands to mov bx,[bp+6]
mov bl, [bx+si]
In addition to Michael's answer, I found the problem - int 10h ah=9 and int 10h ah=2 expect bh as the video page number. After assigning al and bl, I needed to mov bh, 0. The ##Print label is now this:
##Print:
mov ah, 9
mov bx, TextParameter
mov al, [bx+si]
mov bx, MenuColorParameter
mov bl, [bx+si]
mov bh, 0
int 10h
mov ah, 2
inc dl
int 10h
inc si
cmp si, MENU_LEN ; const in DATASEG
jc ##Print
Related
I am trying to make a 8086 program in which I input an array of string and then the program prints them. But the program prints only the 1st characters of input strings and jumbled text.
This is the code where I attempted to do it.
data segment
; Definicija podataka
poruka1 DB "Input array length: $"
strN DB " "
N DW 0
poruka2 DB "Input string: $"
strM DB " "
niz1 DB 16 dup(?)
ends
; Deficija stek segmenta
stek segment stack
dw 128 dup(0)
ends
; Ucitavanje znaka bez prikaza i cuvanja
keypress macro
push ax
mov ah, 08
int 21h
pop ax
endm
; Isis stringa na ekran
writeString macro s
push ax
push dx
mov dx, offset s
mov ah, 09
int 21h
pop dx
pop ax
endm
; Kraj programa
krajPrograma macro
mov ax, 4c02h
int 21h
endm
code segment
; Novi red
novired proc
push ax
push bx
push cx
push dx
mov ah,03
mov bh,0
int 10h
inc dh
mov dl,0
mov ah,02
int 10h
pop dx
pop cx
pop bx
pop ax
ret
novired endp
; Ucitavanje stringa sa tastature
; Adresa stringa je parametar na steku
readString proc
push ax
push bx
push cx
push dx
push si
mov bp, sp
mov dx, [bp+12]
mov bx, dx
mov ax, [bp+14]
mov byte [bx] ,al
mov ah, 0Ah
int 21h
mov si, dx
mov cl, [si+1]
mov ch, 0
kopiraj:
mov al, [si+2]
mov [si], al
inc si
loop kopiraj
mov [si], '$'
pop si
pop dx
pop cx
pop bx
pop ax
ret 4
readString endp
; Konvertuje string u broj
strtoint proc
push ax
push bx
push cx
push dx
push si
mov bp, sp
mov bx, [bp+14]
mov ax, 0
mov cx, 0
mov si, 10
petlja1:
mov cl, [bx]
cmp cl, '$'
je kraj1
mul si
sub cx, 48
add ax, cx
inc bx
jmp petlja1
kraj1:
mov bx, [bp+12]
mov [bx], ax
pop si
pop dx
pop cx
pop bx
pop ax
ret 4
strtoint endp
; Konvertuje broj u string
inttostr proc
push ax
push bx
push cx
push dx
push si
mov bp, sp
mov ax, [bp+14]
mov dl, '$'
push dx
mov si, 10
petlja2:
mov dx, 0
div si
add dx, 48
push dx
cmp ax, 0
jne petlja2
mov bx, [bp+12]
petlja2a:
pop dx
mov [bx], dl
inc bx
cmp dl, '$'
jne petlja2a
pop si
pop dx
pop cx
pop bx
pop ax
ret 4
inttostr endp
start:
; postavljanje segmentnih registara
ASSUME cs: code, ss:stek
mov ax, data
mov ds, ax
; Mesto za kod studenata
writeString poruka1
; Unos broja elemenata
push 3
push offset strN
call readString
; Pretvaranje strN u broj
push offset strN
push offset N
call strtoint
call novired
writeString strN
; Unos elemenata niza
mov cx, N
mov di, 0
mov si, 0
unos:
call novired
writeString poruka2
push 6
push offset strM
call readString
mov al, strM
mov niz1[di], al
add di, 2
add si, 1
loop unos
call novired
mov cx, N
mov di, 0
mov si, 0
ispis:
writeString niz1[di]
add di, 2
add si, 1
loop ispis
krajPrograma
ends
end start
I input an array of string and then the program prints them. But the program prints only the 1st characters of input strings ...
What else could the program print if the 1st character is all that you store in memory?
mov al, strM
mov niz1[di], al
add di, 2
... and jumbled text.
With add di, 2 you leave an undetermined byte between any two of these 1st characters. That's not going to look nice. Make that byte the DOS string terminator $.
Your writeString macro does not load an address but a word from memory. That's not going to work. The invokation writeString niz1[di] gets expanded into mov dx, offset niz1[di]. What you need here is LEA instead of MOV.
Try the following:
....
unos:
call novired
writeString poruka2
push 6
push offset strM
call readString
mov al, strM
MOV AH, "$"
mov niz1[di], AX
add di, 2
loop unos
call novired
mov cx, N
mov di, 0
ispis:
LEA DX, niz1[di]
MOV AH, 09h
INT 21h
add di, 2
loop ispis
...
i want to read from the middle of the file(1127k) but i cant because i can mov to cx just 65200
because it is word
this is the code:
mov ah, 3Dh
mov al, 0
mov dx,offset FileName
int 21h
mov [Handle], ax
mov ah, 42h
mov al, 00
mov bx, [Handle]
mov cx, 0
mov dx, 65200
int 21h
jc ##Err
i thought that i can use edx but i dont know how i do that int 21h will use edx and not dx
tnx for the helpers :)
This code doesn't work for me. My goal is to ask user string input, convert to capital letters, and store them one by one in an array, then output the characters(that is in all caps) back to the user. please help me :(
org 100h
mov bl, 0
mov ah, 9
mov dx, input
int 21h
again:
mov ah, 1
int 21h
sub al, 20h
mov [inp+bl], al
inc bl
cmp bl, 2
jle again
loops:
mov bl, 0
mov ah, 2
mov dl, [inp+bl]
int 21h
inc bl
cmp bl, 2
jle loops
mov ax, 4Ch
int 21h
input db 'Input: ',24h
output db 'Output: ',24h
inp db 20 dup(?), 24h
mov [inp+bl], al
The main problem here is that you use an addressing mode that simply does not exist!
You can quickly correct your code if you change every instance of BL into BX.
mov bx, 0
mov ah, 9
mov dx, input
int 21h
again:
mov ah, 1
int 21h
sub al, 20h
mov [inp+bx], al
inc bx
cmp bx, 2
jle again
loops:
mov bx, 0
mov ah, 2
mov dl, [inp+bx]
int 21h
inc bx
cmp bx, 2
jle loops
sub al, 20h
Perhaps you've over-simplified the code because this capitalization will of course only work if the user only types in small caps a..z and nothing else.
I'v got a a task to write a program which would create below as output:
ABCD******
ABCD***
ABCD
My code is:
kod segment
org 100h
assume cs:kod
start:
mov cx,3
mov bx,3
mov ax,6
mov ah, 02h
PETLA_ZEW:
push cx
mov cx,4
mov dl, 'A'
PETLA_CIAG:
int 21h
inc dl
dec cx
jnz PETLA_CIAG
push ax
mov cx, ax
mov dl, '*'
PETLA_GWIAZD:
int 21h
dec cx
jnz PETLA_GWIAZD
sub ax, 3
mov dl, 0ah
int 21h
mov dl, 0dh
int 21h
push bx
mov cx, bx
mov dl, ' '
PETLA_SPACJE:
int 21h
dec bx
jnz PETLA_SPACJE
pop bx
add bx, 3
pop cx
dec cx
jnz PETLA_ZEW
koniec:
mov ah, 4ch
int 21h
kod ends
end start
but the problem is that in loop called PETLA_GWIAZD when I subtract ax 2nd time it's value is 0, which is causing the infinite loop. Is there any other loop I should use when subtracting? Maybe some validation check just before the loop? I just started working with Assembly and still do not know a lot...
Thanks!
The problem is the registers you are using : AX and CX are used for interrupts and loops, so you can use other registers, for example, SI instead of CX and DI instead of AX :
kod segment
org 100h
assume cs:kod
start:
mov si,3 ;◄■■ INSTEAD OF CX.
mov bx,3
mov di,6 ;◄■■ INSTEAD OF AX.
mov ah, 02h
PETLA_ZEW:
mov cx,4
mov dl, 'A'
PETLA_CIAG:
int 21h
inc dl
dec cx
jnz PETLA_CIAG
cmp di, 0 ;◄■■ IF COUNTER == 0 ...
je koniec ;◄■■ ... NO MORE ASTERISKS.
mov cx, di ;◄■■
mov dl, '*'
PETLA_GWIAZD:
int 21h
dec cx
jnz PETLA_GWIAZD
sub di, 3 ;◄■■
mov dl, 0ah
int 21h
mov dl, 0dh
int 21h
push bx
mov cx, bx
mov dl, '_'
PETLA_SPACJE:
int 21h
dec bx
jnz PETLA_SPACJE
pop bx
add bx, 3
dec si ;◄■■
jnz PETLA_ZEW
koniec:
mov ah, 4ch
int 21h
kod ends
end start
I am new to assembly language and was asked to have a program that will allow only three chances of error when typing the password in.
s:
main proc
mov ax, #data
mov ds, ax
mov cx, 3
again:
mov ah, 9
lea dx, a
int 21h
mov ah, 2
mov dl, '*'
int 21h
mov ah, 8
int 21h
mov bl, al
mov ah, 2
cmp bl, 'Y'
je second
cmp bl, 'y'
je second
jne iinvalid
second:
mov ah, 2
mov dl, '*'
int 21h
mov ah, 8
int 21h
mov bh, al
mov ah, 2
cmp bh, 'E'
je third
cmp bh, 'e'
je third
jne iinvalid
third:
mov ah, 2
mov dl, '*'
int 21h
mov ah, 8
int 21h
mov cl, al
mov ah, 2
cmp cl, 'S'
je welcome
cmp cl, 's'
je welcome
jne iinvalid
iinvalid:
jmp invalid
checking:
mov ah, 9
lea dx, i
int 21h
jmp exit
togo:
loop again
welcome:
mov ah,9
lea dx, c
int 21h
lea dx, d
int 21h
mov ah, 1
int 21h
mov bl, al
newp:
mov ah, 9
lea dx, i
int 21h
jmp exit
invalid:
mov ah, 9
lea dx, b
int 21h
mov ah, 1
int 21h
mov cl, al
cmp cl, 'Y'
je togo
cmp cl, 'y'
je togo
jne exit
exit:
mov ah, 4ch
int 21h
main endp
end s
you destroy the content of cx inside the loop.
easiest way to fix it:
mov cx, 3
again:
push cx ; save loop cnt on stack
mov ah, 9
...
togo:
pop cx ; restore cnt from stack
loop again
keep in mind, that if you jump out of the loop, cx must still be removed from stack
welcome:
pop cx ; purge cnt from stack
mov ah,9
just a hint:
you compare the characters entered, to be either lowercase, or uppercase
uppercase and lowercase characters are (in ascii) identically, except of bit 0x20. you could "mask out" this bit by an AND with 0xDF ( 1101 1111 binary), and check the characters with just a single branch (instead of 3):
cmp bl, 'Y'
je second
cmp bl, 'y'
je second
jne iinvalid
--->
and bl, 0xdf ; make lowercase to uppercase
cmp bl, 'Y' ; THEN here both 'y' and 'Y' match
jne iinvalid