在装配体x86中打印数字的三角形 [英] Print triangle of numbers in assembly x86
问题描述
我必须用tasm编写程序,该程序必须在这样的数字上打印一个三角形:
input: n. Ex: n=4
output:
1
1 2
1 2 3
1 2 3 4
我已经设法使程序打印出该内容,但是我还必须使其在0到255之间的数字下工作,而不仅仅是数字.
我知道我必须逐位读取一个数字并创建一个像这样的总和:
如果我必须读82,我首先要读8,将其放入寄存器中,然后当我读2时,必须将其加到8 * 10.
您能帮我在程序中实现吗?
.model small
.stack 100h
.data
msg db "Enter the desired value: $", 10, 13
nr db ?
.code
mov AX, @data
mov DS, AX
mov dl, 10
mov ah, 02h
int 21h
mov dl, 13
mov ah, 02h
int 21h
mov DX, OFFSET msg
mov AH, 9
int 21h
xor ax, ax
mov ah, 08h ;citire prima cifra din numar
int 21h
mov ah, 02h
mov dl, al
int 21h
sub al,30h
mov ah,10
mul ah
mov [nr],al ;mutam prima cifra inmultita cu 10 in nr
mov ah, 08h
int 21h
mov ah, 02h
mov dl, al
int 21h
sub al, 30h
add [nr], al
sub nr,30h
mov dl, 10
mov ah, 02h
int 21h
mov dl, 13
mov ah, 02h
int 21h
mov cx,1
mov bx,31h
mov ah, 2
mov dx, bx
int 21h
loop1:
xor ax, ax
mov al, nr
cmp ax, cx
je final
mov dl, 10
mov ah, 02h
int 21h
mov dl, 13
mov ah, 02h
int 21h
mov bx, 0
loop2:
inc bx
add bx,30h
mov ah, 2
mov dx, bx
int 21h
sub bx,30h
cmp bx, cx
jne loop2
inc bx
add bx,30h
mov ah, 2
mov dx, bx
int 21h
inc cx
jmp loop1
final:
mov AH,4Ch ; Function to exit
mov AL,00 ; Return 00
int 21h
end
您已经完成了一半.
如果我必须读82,我首先要读8,将其放入寄存器,然后当我读2时,必须将其加到8 * 10".
对于您读入的每个数字,您不仅必须将第二个数字乘以10,还必须将第三个数字乘以10(并且您可以对第一个数字乘以10(因为读取的值为0),所以您可以对第一个数字乘以10)>
剩下的是:
result = 0
while (more digits to come)
result *= 10;
result += value of current digit
这样做,
82将是((0 * 10 + 8)+ 2)= 82
251将是((((0 * 10 + 2)* 10 + 5)* 10 +1 = 251
与在循环中输出数字相同,对于> 9的值,您不能简单地添加'0'并打印ascii值,您必须将其编码为ascii字符串,并显示整个字符串(就像您已经做过的一样) INT 21H/09H)
也请帮自己一个忙,并消除这些问题.编写一个"decode_bin"和"encode_bin"子函数,并用对此函数的调用替换循环中的INT,否则,大约两周后您将无法读取它:-)
I must do a program in tasm which have to print a triangle on numbers like this:
input: n. Ex: n=4
output:
1
1 2
1 2 3
1 2 3 4
I've managed to make my program print this thing, but i also have to make it work with numbers between 0 and 255, not only for digits.
I know that i have to read a number digit by digit and create a sum like this:
if i have to read 82, i read firstly 8, put it in a register, and then, when i read 2, it must be added to 8*10.
Can you help me to implement this in my program?
.model small
.stack 100h
.data
msg db "Enter the desired value: $", 10, 13
nr db ?
.code
mov AX, @data
mov DS, AX
mov dl, 10
mov ah, 02h
int 21h
mov dl, 13
mov ah, 02h
int 21h
mov DX, OFFSET msg
mov AH, 9
int 21h
xor ax, ax
mov ah, 08h ;citire prima cifra din numar
int 21h
mov ah, 02h
mov dl, al
int 21h
sub al,30h
mov ah,10
mul ah
mov [nr],al ;mutam prima cifra inmultita cu 10 in nr
mov ah, 08h
int 21h
mov ah, 02h
mov dl, al
int 21h
sub al, 30h
add [nr], al
sub nr,30h
mov dl, 10
mov ah, 02h
int 21h
mov dl, 13
mov ah, 02h
int 21h
mov cx,1
mov bx,31h
mov ah, 2
mov dx, bx
int 21h
loop1:
xor ax, ax
mov al, nr
cmp ax, cx
je final
mov dl, 10
mov ah, 02h
int 21h
mov dl, 13
mov ah, 02h
int 21h
mov bx, 0
loop2:
inc bx
add bx,30h
mov ah, 2
mov dx, bx
int 21h
sub bx,30h
cmp bx, cx
jne loop2
inc bx
add bx,30h
mov ah, 2
mov dx, bx
int 21h
inc cx
jmp loop1
final:
mov AH,4Ch ; Function to exit
mov AL,00 ; Return 00
int 21h
end
you're half way done.
"if i have to read 82, i read firstly 8, put it in a register, and then, when i read 2, it must be added to 8*10"
For EACH digit you read in, you have to multiply the previous value by 10. not only for the 2nd, also for the 3rd (and you can do this for the first, since the value read there was 0)
what remains is:
result = 0
while (more digits to come)
result *= 10;
result += value of current digit
doing this,
82 will be ((0*10 + 8) + 2) = 82
251 will be (((0*10 + 2) *10 + 5) *10 +1 = 251
same with outputting your numbers in the loop, for values > 9, you cannot simply add '0' and print the ascii value, you have to encode it as an ascii string, and display the whole string ( like you already did with INT 21H/09H )
Also do yourself a favour, and divide these problems. Write a "decode_bin" and a "encode_bin" sub-functions, and replace the INTs in your loop with calls to this functions, otherwise you'll not ba able to read it, after 2 weeks or so :-)
这篇关于在装配体x86中打印数字的三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!