Using loop in assembly language using DOSBOX - loops

Hello I need to show the output like this
9_8_7_6_5_4_3_2_1_0
But I am having a difficulty to store the "underscore" temporarily and as I notice registers like DH, CH, BH, BL can't be use to output using int int 21H. Here is my code
.model small
.stack
.data
.code
begin:
mov ah, 2
mov cx, 10
mov dl, 39h
int 21h
back: mov dl, 5fh
int 21h
sub dl, 39
int 21h
loop back
mov ah,4ch
int 21h
end begin

You can use another register to store the counter (9..0), for example, bl :
.model small
.stack
.data
.code
begin:
mov ah, 2
mov cx, 10
mov bl, '9' ;◄■■ COUNTER 9..0.
back:
mov dl, bl ;◄■■ MOVE COUNTER INTO DL.
int 21h ;◄■■ DISPLAY COUNTER.
dec bl ;◄■■ COUNTER-1.
mov dl, 5fh ;◄■■ MOVE UNDERSCORE INTO DL.
int 21h ;◄■■ DISPLAY UNDERSCORE.
loop back
mov ah,4ch
int 21h
end begin

Related

Retrieve data from an array

So, basically I just want to display the first data from array which is userbalanceIndex1 from userbalanceIndex.
But I get an error which is
DEMO.ASM(29): Out of memory
29 line: mov bl,(userbalanceIndex +1)
Here is my code:
.model small
.stack 100H
.data
userbalanceIndex db usrbalance1, usrbalance2, usrbalance3, usrbalance4
usrbalance1 Db 1
usrbalance2 Db 9
usrbalance3 Db 6
usrbalance4 Db 0
.code
OUTPUT MACRO DIGIT
MOV AH, 02H
MOV DL, DIGIT
INT 21H
ENDM
main proc
MOV AX,#DATA
MOV DS,AX
mov al, userbalanceIndex
mov cx, 0
a:
inc userbalanceIndex
mov bl, (userbalanceIndex + 1)
loop a
OUTPUT bl
mov ah, 4ch
int 21h
main endp
end main

FASM: How to input values in array using loop

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.

Assembly loop char

I want to use the LOOP instruction to print this string in assembly :
abcccbcccabcccbccc
We have ab, ccc, b and then backup to ab and then ccc.
I hope I will find some solutions with your help.
I am using emu to compile the source !
.model small
.stack 200h
.code
main PROC
mov ah, 0
mov al, 12h ; Clear screen
int 10h
mov ah, 3
mov bh, 0 ; get cursor
int 10h
mov ah, 2
mov bh, 0 ;set cursor
mov dl,12
int 10h
mov cx, 5 ; counter
mov dl, 65 ; ASCII of 'A'
t1:
mov ah, 2h
int 21h
add dl, 32 ; 97 - 65 - convert to LC
mov ah, 2h
int 21h
sub dl,31 ;remove the 32 added, but increment
push dx ;save DX on stack
mov dl, 32 ;space character
mov ah, 2h
int 21h
pop dx ;return DX from stack
loop t1
mov ah, 4Ch ;exit DOS program
mov al, 00h ;return code = 0
int 21h
ENDP
END main
Next 2 small programs are 2 possible solutions to your problem :
SOLUTION #1
.stack 100h
.data
txt db 'abcccbccc$' ;STRING TO DISPLAY.
.code
mov ax, #data ;INITIALIZA DATA SEGMENT.
mov ds, ax
mov cx, 3 ;DISPLAY STRING 3 TIMES.
t1:
mov ah, 9
mov dx, offset txt
int 21h
loop t1
mov ax, 4c00h ;TERMINATE PROGRAM.
int 21h
SOLUTION #2
.stack 100h
.data
s1 db 'ab$' ;STRING TO DISPLAY.
s2 db 'ccc$' ;STRING TO DISPLAY.
s3 db 'b$' ;STRING TO DISPLAY.
.code
mov ax, #data ;INITIALIZA DATA SEGMENT.
mov ds, ax
mov cx, 12 ;DISPLAY THE 4 STRING 3 TIMES.
mov bl, 1 ;1 = DISPLAY STRING S1.
;2 = DISPLAY STRING S2.
;3 = DISPLAY STRING S3.
;4 = DISPLAY STRING S2.
mov ah, 9 ;NECESSARY TO DISPLAY STRINGS.
t1:
;BL TELLS WHAT TO DO.
cmp bl, 1
je string1 ;BL=1 : DISPLAY S1.
cmp bl, 2
je string2 ;BL=2 : DISPLAY S2.
cmp bl, 3
je string3 ;BL=3 : DISPLAY S3.
;string4: ;BL=4 : DISPLAY S2.
mov dx, offset s2
int 21h
jmp continue ;SKIP NEXT DISPLAYS.
string1:
mov dx, offset s1
int 21h
jmp continue ;SKIP NEXT DISPLAYS.
string2:
mov dx, offset s2
int 21h
jmp continue ;SKIP NEXT DISPLAYS.
string3:
mov dx, offset s3
int 21h
continue:
inc bl ;BL TELLS WHAT TO DO.
cmp bl, 4
jbe nextstring ;IF BL < 4
mov bl, 1 ;BL STARTS AGAIN.
nextstring:
loop t1
mov ax, 4c00h ;TERMINATE PROGRAM.
int 21h

Assembly program to print 'a' to 'z' using stack, loop cx and dx

I've got an assignment to make an assembly program which print 'a' to 'z' vertically line by line using stack, loop cx and dx.
Can anyone help me please.
This is program of printing a to z, but i dont have an idea how to use stack cx and dx in my program:
.data
l1c db 0ah,0dh,"S"
.code
main proc
mov ax #data
mov ds,ax
mov al,48
mov cx,10
d:
mov dl,al
mov ah,2
int 21h
call linechange
inc al
loop d
mov ah,4ch
int 21h
main endp
;Procedure
linechange proc
lea dx,l1c
mov ah,9
int 21h
ret
linechnage endp
end main
(this is the program which i made by my self)
I guess your teacher wanted to avoid a "clever" solution like:
.MODEL SMALL
.STACK 1000h
.DATA
smart_out db 'a',13,10,'b',13,10,'b',13,10,'c',13,10,'d',13,10,'e',13,10
db 'f',13,10,'g',13,10,'h',13,10,'i',13,10,'j',13,10,'k',13,10
db 'l',13,10,'m',13,10,'n',13,10,'o',13,10,'p',13,10,'q',13,10
db 'r',13,10,'s',13,10,'t',13,10,'u',13,10,'v',13,10,'w',13,10
db 'x',13,10,'y',13,10,'z',13,10,'$'
.CODE
main:
mov ax, #data
mov ds, ax
mov ah, 9
mov dx, OFFSET smart_out
int 21h
mov ax, 4C00h
int 21h
END main
You are supposed to output this with a LOOP (CX needed). In a loop you need to preserve at least AX and CX since you never know if a procedure like INT 21h will change it. A call to INT 21h / 9 fulfills the third condition since it needs a value in DX:
.MODEL SMALL
.STACK 1000h ; Reserve space for stack and initialize stack pointer
.DATA
l1c db 0dh, 0ah, '$' ; Dollar-sign!
.CODE
main PROC
mov ax, #data
mov ds, ax
mov al, 'a'
mov cx, 26
d:
push ax ; Store AX (AL is a part of AX)
push cx ; Store CX
mov dl, al
mov ah, 2
int 21h
mov ah, 9
mov dx, OFFSET l1c
int 21h
pop cx ; Restore CX & AX in reversed push-order
pop ax
inc al
loop d ; Loops until cx == 0
mov ah, 4ch
int 21h
main ENDP
END main

Loop and Constant in Assembly

I just want to ask a question.
How can I display an output like this "0_1_2_3_4_5_6_7_8_9"
And I need to use a loop in the numbers, but how can I make underscore constant in every loop?
Here is my working codes.
.model small
.stack 200h
.code
main proc
mov ah, 0
mov al, 12h ; Clear screen
int 10h
mov ah,3
mov bh,0 ; get cursor
int 10h
mov ah,2
mov bh,0 ;set cursor
mov dl,12
int 10h
mov cx, 9 ; counter
Mov ah, 2
Mov dl, 48 ; display 0
top:
int 21h
add dl, 47 ; display underscore
mov ah, 2
int 21h
push dx
add dl, -46 ; return to 1
mov ah, 2
int 21h
pop dx
loop top
mov ah, 4ch
mov al,00h
int 21h
endp
end main
I always result in this, please click here
Please help me.
Thanks.
This is relative to the current digit, which will give you incorrect results for anything except when the digit is '0':
add dl, 47 ; display underscore
mov ah, 2
int 21h
You're also pushing and popping dx in the wrong places (relative to where you're changing its value).
A better approach would be:
top:
int 21h ; print digit
push dx ; save dx
mov dl,'_'
mov ah, 2
int 21h ; print underscore
pop dx ; restore dx
inc dl ; next digit
loop top
This one works fine:
; 0_9.asm
; assemble with "nasm -f bin -o 0_9.com 0_9.asm"
org 0x100 ; .com files always start 256 bytes into the segment
mov cx, 9 ; counter
mov dl, "0" ; 0
top:
push dx
push cx
mov ah, 2
int 21h
mov dl, "_" ; display underscore
mov ah, 2
int 21h
pop cx
pop dx
inc dl
loop top
mov dl, "9" ; display 9
mov ah, 2
int 21h
mov ah, 4ch ; "terminate program" sub-function
mov al,00h
int 21h

Resources