从键盘Assembler读取数组的元素

时间:2016-03-31 19:43:52

标签: arrays sorting assembly long-integer tasm

我是汇编语言的新手,从事TASM工作,需要按升序对数字进行排序。代码很简单,但我必须从键盘读取数组元素的数量,然后从键盘插入它们。问题是你键入的所有内容都是用ASCII代码看到的,我对转换一无所知。我该怎么读?

提前谢谢!

排序5个数字

.MODEL SMALL 
.DATA 
NUM DW 42H,34H,26H,17H,09H 
LEN EQU 05H

.CODE 
START: 
MOV AX,@DATA 
MOV DS,AX 
MOV BX,LEN-1  ;Initialize BX as the counter for loop UP 
MOV CX,BX     ;Initialize CX as the counter for loop UP1

UP1: MOV BX,CX 
LEA SI,NUM    ;SI points to the first number in NUM

UP: MOV AX,[SI] 
MOV DX,[SI+2] 
CMP AX,DX 
JB DOWN         ;no swapping if [SI] less than [SI+2] 
MOV [SI],DX 
MOV [SI+2],AX   ;swap the numbers pointed by SI and SI+2 
                ;if [SI] is greater than [SI+2]

DOWN:
INC SI 
INC SI       ;increment SI to point to next dataword(number) 
DEC BX 
JNZ UP 

DEC CX 
JNZ UP1 

MOV AH,4CH 
INT 21H 
END START 
END

1 个答案:

答案 0 :(得分:0)

你在字符串中读取并转换它 - char by char。 算法可以像这样描述

for each digit in String
   value *= 10
   value += value(digit)

生病给你一个样品:

string "4132"

"4" is a digit, so value = 10*value (=   0) + digit (=   4)
"1" is a digit, so value = 10*value (=  40) + digit (=  41)
"3" is a digit, so value = 10*value (= 410) + digit (= 413)
"2" is a digit, so value = 10*value (=4130) + digit (=4132)
no more digits, we're done