问题
My question is related to printing an array in assembly 8086 language. I use 'emu8086' program.
The following piece seems fine to me (I'm a beginner), yet the result I get is: *P000, instead of: 12345.
Main:
A DB 1,2,3,4,5 //my array
SUB SI, SI //SI stands for counter and index here
LEA BX, A
loop3:
MOV DX, [BX + SI]
ADD DX, 30h //converting digit into character
MOV Ah, 2h
int 21h //displaying the character in console window
INC SI
CMP SI, 5
JNE loop3
end Main
Can you, please, explain what's wrong with my function ? Thank you in advance !
回答1:
The program, in the question, is incomplete. There are two vital lines missing:
MOV AX, @DATA
MOV DS, AX
Here I found the purpose of these.
Below are listed the things that made me change the program.
- I found a good assembly program on this topic, based on which, step by step, I could analyze each line of code, and understand the meaning. I think this program explains everything.
There are some things I discovered:
Assembly language program should have a structure
Each register has its own purpose
So my program now looks this way:
.MODEL SMALL
.STACK 100H
.DATA
A DW 1, 2, 3, 4 ; it's my array
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
LEA SI, A ;set SI as offset address of A (of my array)
MOV BX, 4 ;store the number of digits in BX register
MOV CX, BX ;CX is used as a loop counter
LOOP1:
MOV DX, [SI] ; Line 1
ADD DX, 30h ;convert digit to char and store in DX
;Print character stored in DX
MOV AH, 2h
INT 21h
;Store in DX the ASCII code for 'space' character
MOV DX, 20h
;Print ' ' = space
INT 21h
ADD SI, 2 ;SI = SI + 2
LOOP LOOP1 ; jump to label 'LOOP1' while CX != 0
MAIN ENDP
- The meaning for Line 1 I found here.
- Here I found what instructions to use for printing characters, with all the explanations.
- The ASCII Table was helpful.
- About LOOP instruction.
回答2:
You'll want to verify that the DS
register is loaded with the proper value, or your memory reads will be from the wrong segment.
回答3:
.MODEL SMALL
.DATA
ARRAY DW 1,2,3,4,5
.CODE
.STARTUP
MOV CX,5
MOV SI,OFFSET ARRAY
LOP:
MOV DX,[SI]
ADD DX,30H
MOV AH,2H
INT 21H
INC SI
INC SI
LOOP LOP
.EXIT
END
TRY THIS
来源:https://stackoverflow.com/questions/35709437/printing-array-getting-strange-output-emu8086