我很难完成这项家庭作业。我需要打印我的数组,显示我从ReadInt中提取的数字都在其中。我不知道该怎么做。 到目前为止,这是我的代码。
.data
intarray DWORD ?
finish BYTE "Please enter a EVEN number less than 50. ", 0dh, 0ah, 0
finish2 BYTE "The entered array is: ", 0dh, 0ah, 0
finish3 BYTE "The sum of the first half of the array is: ", 0dh, 0ah, 0
finish4 BYTE "Please enter your numbers: ", 0dh, 0ah, 0
.code
main proc
mov edx, OFFSET finish
call WriteString
call crlf
call ReadInt
add intarray, eax
call crlf
mov edx, OFFSET finish4
call WriteString
call crlf
mov ecx,intarray
L1:
call ReadInt
mov intarray, eax
inc intarray
loop L1
mov edx, OFFSET finish2
call WriteString
call crlf
invoke ExitProcess,0
main endp
end main
我需要打印我的intarray以显示ReadInt输入的数字是否在其中。我不知道该怎么做。
答案 0 :(得分:0)
下一代码可以满足您的需求。代码是用EMU8086编写的:它将10个数字作为字符串读取(因为数据来自键盘),将每个字符串转换为数字,将它们插入数组,清除屏幕,并在数组中显示数字,将每个数字转换为字符串(显示它们)。这里有很多评论可以帮助您理解(希望这会对您有所帮助):
.stack 100h
;------------------------------------------
.data
;------------------------------------------
array dw 10 dup(?)
msj db 13,10,'Enter a number: $'
str db 6 ;MAX NUMBER OF CHARACTERS ALLOWED (5).
db ? ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
db 6 dup (?) ;CHARACTERS ENTERED BY USER.
buf db 6 dup('$') ;WILL HOLD NUMBERS WITH 5 DIGITS OR LESS.
crlf db 13,10,'$'
count db ? ;JUST A COUNTER FOR LOOPS.
;------------------------------------------
.code
;INITIALIZE DATA SEGMENT.
mov ax, @data
mov ds, ax
;------------------------------------------
;CAPTURE 10 NUMBERS FROM USER.
mov di, offset array ;POINTER TO ARRAY.
mov count, 10 ;WE WILL CAPTURE 10 NUMBERS.
ten_numbers:
;DISPLAY MESSAGE.
mov ah, 9
mov dx, offset msj
int 21h
;CAPTURE NUMBER AS STRING.
mov ah, 0Ah
mov dx, offset str
int 21h
;CONVERT CAPTURED NUMBER FROM STRING TO NUMERIC.
mov si, offset str ;PARAMETER FOR STRING2NUMBER.
call string2number ;NUMBER RETURNS IN BX.
;INSERT NUMBER INTO ARRAY.
mov [ di ], bx ;NUMBER GOES IN CURRENT POSITION (DI).
add di, 2 ;ARRAY POSITION FOR THE NEXT NUMBER. MUST BE
;2 BECAUSE IT'S AN ARRAY OF DW (DW = TWO BYTES).
;REPEAT PROCESS.
dec count
jnz ten_numbers ;IF ( COUNT != 0 ) JUMP.
;------------------------------------------
;DISPLAY THE NUMBERS IN ARRAY.
call clear_screen
mov di, offset array
mov count, 10 ;ARRAY HAS 10 NUMBERS.
print_array:
;CONVERT NUMBER TO STRING TO DISPLAY IT.
call dollars ;FILLS BUF WITH DOLLARS (REQUIRED TO DISPLAY).
mov ax, [ di ] ;MOVE CURRENT NUMBER TO AX.
call number2string ;TAKES AX AS PARAMETER.
;STRING RETURNS IN VARIABLE BUF.
;DISPLAY STRING.
mov ah, 9
mov dx, offset buf
int 21h
;DISPLAY LINE BREAK.
mov ah, 9
mov dx, offset crlf
int 21h
;REPEAT PROCESS.
add di, 2 ;NEXT NUMBER IN ARRAY TO BE DISPLAYED. MUST BE
;2 BECAUSE IT'S AN ARRAY OF DW (DW = TWO BYTES).
dec count
jnz print_array ;IF ( COUNT != 0 ) JUMP.
;------------------------------------------
;WAIT FOR USER TO PRESS ANY KEY.
mov ah,7
int 21h
;------------------------------------------
;FINISH THE PROGRAM.
mov ax, 4c00h
int 21h
;==========================================
;NUMBER TO CONVERT MUST ENTER IN AX.
;ALGORITHM : EXTRACT DIGITS ONE BY ONE, STORE
;THEM IN STACK, THEN EXTRACT THEM IN REVERSE
;ORDER TO CONSTRUCT STRING (BUF).
proc number2string
mov bx, 10 ;DIGITS ARE EXTRACTED DIVIDING BY 10.
mov cx, 0 ;COUNTER FOR EXTRACTED DIGITS.
cycle1:
mov dx, 0 ;NECESSARY TO DIVIDE BY BX.
div bx ;DX:AX / 10 = AX:QUOTIENT DX:REMAINDER.
push dx ;PRESERVE DIGIT EXTRACTED FOR LATER.
inc cx ;INCREASE COUNTER FOR EVERY DIGIT EXTRACTED.
cmp ax, 0 ;IF NUMBER IS
jne cycle1 ;NOT ZERO, LOOP.
;NOW RETRIEVE PUSHED DIGITS.
mov si, offset buf
cycle2:
pop dx
add dl, 48 ;CONVERT DIGIT TO CHARACTER.
mov [ si ], dl
inc si
loop cycle2
ret
endp
;------------------------------------------
;CONVERT STRING TO NUMBER IN BX.
;SI MUST ENTER POINTING TO THE STRING.
proc string2number
;MAKE SI TO POINT TO THE LEAST SIGNIFICANT DIGIT.
inc si ;POINTS TO THE NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;NUMBER OF CHARACTERS ENTERED.
mov ch, 0 ;CLEAR CH, NOW CX==CL.
add si, cx ;NOW SI POINTS TO LEAST SIGNIFICANT DIGIT.
;CONVERT STRING.
mov bx, 0
mov bp, 1 ;MULTIPLE OF 10 TO MULTIPLY EVERY DIGIT.
repeat:
;CONVERT CHARACTER.
mov al, [ si ] ;CHARACTER TO PROCESS.
sub al, 48 ;CONVERT ASCII CHARACTER TO DIGIT.
mov ah, 0 ;CLEAR AH, NOW AX==AL.
mul bp ;AX*BP = DX:AX.
add bx,ax ;ADD RESULT TO BX.
;INCREASE MULTIPLE OF 10 (1, 10, 100...).
mov ax, bp
mov bp, 10
mul bp ;AX*10 = DX:AX.
mov bp, ax ;NEW MULTIPLE OF 10.
;CHECK IF WE HAVE FINISHED.
dec si ;NEXT DIGIT TO PROCESS.
loop repeat ;COUNTER CX-1, IF NOT ZERO, REPEAT.
ret
endp
;------------------------------------------
;FILLS VARIABLE BUF WITH '$'.
;USED BEFORE CONVERT NUMBERS TO STRING, BECAUSE
;THESE STRINGS WILL BE DISPLAYED.
proc dollars
mov si, offset buf
mov cx, 6
six_dollars:
mov al, '$'
mov [ si ], al
inc si
loop six_dollars
ret
endp
;------------------------------------------
proc clear_screen
mov ah,0
mov al,3
int 10H
ret
endp
也许重要的是解释一下变量 str ,注意三级格式,它使用3个DB,因为啊= 0Ah(捕获字符串)需要它:第一个DB必须指示最大数量允许的字符数,第二个DB是存储长度所必需的,第三个DB是字符本身。字符串以ENTER结尾(字符13),这就是第一个DB为6的原因(如果用户输入5个字符,第六个是ENTER)。