问题
I have just started to learn assembly language and i am trying to print "hello world" in reverse order that means "dlrow olleh".the problem is i am getting only 1st letter as output and the order is still same no change at all!As a newbie many thing is unknown to me and i am doing lots of mistakes and i am unable to identify them due to lack of knowledge.So any answer with proper explanation will be appreciated!Here is my code:
name "hi" ; can anybody explain what is the use of this?
org 100h
jmp start ; jump over data declaration
msg db "1Hello, World!",0;last character
msg1 db "1"
Mov SI,13;lenght of the string
start:
Mov AL,msg[SI]
DEC SI
Mov ah ,0eh
int 10h
mov BL,msg1
CMP msg[SI],BL;comparing to get the end of the string
je stop
jmp start
stop:
mov ah, 0
int 16h ; wait for any key....
ret ; return to operating system.
i am getting output only "1" which is the first letter,but i expected to get the whole string in reverse order
回答1:
jmp start ; jump over data declaratio
...
Mov SI,13;lenght of the string
start:
here is the problem - you're not initializing register si
you need to use something like:
jmp init ; jump over data declaratio
...
init:
Mov SI,13;lenght of the string
start:
回答2:
This NOT optimized for machine efficiency.
It is presented to simplify your problem so that you can better see the logic.
Item #1: Comment each instruction, you'll do yourself a favor.
Here, try a program structure with a loop, something like this...
Lea Si, msg ;The target string
Mov Cx, 13 ;The length of the string (be careful, but it'll probably work)
Mov Bx, Cx ;Think: the "base" can actually be an "offset into"
The_Backwards_Loop:
Mov AL,[Bx+Si] ;Get the "BXth" character in the string
Mov AH,0Eh ;Code for Bios to put that out to the screen
Int 10h ;Bios puts char in AL onto the screen
Dec Bx ;Bx was at the Nth character, is now at N-1
Loop The_Backwards_Loop ;And away we go through the rest of the string
;Note that Cx will dec just like we did Bx
Mov Ah, 0 ;Code to wait for a keystroke
Int 16h ;OS will now wait
Ret ;And we are done, messing up Si, Bx, Ax, Cx,
My suggestion for learning this stuff...
- Debug this with single stepping
- You'll get the logic in your head
- At that point, think about "efficiency"
- Try any optimizations one at the time
As for this being "inefficient", yes, it is.
Anything that works is more efficient than anything that doesn't.
Again, the purpose of this example/suggestion is to simplify the logic and code structure first, so that you can get a clearer picture in your mind; then you can "optimize"
回答3:
A simple way to reverse a string are give below:
INCLUDE 'EMU8086.INC'
.MODEL SMALL
.STACK
.DATA
ARR DB 10 DUB (?)
;STR DB 0AH,0DH,'OUTPUT: $'
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
XOR BX, BX
MOV CX, 0
FOR:
MOV AH, 1
INT 21H
CMP AL,0DH
JE PRINT
MOV ARR[BX], AL
INC BX
INC CX
JMP FOR
PRINT:
MOV AH, 2 ;new line
MOV DL, 0AH
INT 21H
MOV DL, 0DH ;curage return
INT 21H
BACK:
CMP CX, 0
JE FINISH
DEC CX
DEC BX
MOV AL, ARR[BX]
MOV AH, 2
MOV DL, AL
INT 21H
JMP BACK
FINISH:
MOV AH,4CH
INT 21H
MAIN ENDP
来源:https://stackoverflow.com/questions/26368586/how-to-print-a-string-in-reverse-order-in-a-com-executable