2

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

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
user2252617
  • 99
  • 1
  • 4
  • 14

3 Answers3

2
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: 
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
  • I agree that is definitely a problem. I would have expected different behavior, though. – Robert Harvey Oct 14 '14 at 19:23
  • If SI has the value 0, that is exactly the behavior I would expect. I'm not sure if it is guaranteed to be 0, or to have some non-deterministic value. – Eric J. Oct 14 '14 at 19:28
  • @RobertHarvey si usually will be 0, so this app will display "1" on most computers, but later is not very clear - `dec si` will make it 0xffff and will compare `jmp` and then last bytes of PSP with bl, I suppose last bytes will be all zeros, so they are displayed, but not visible and then finally this loop can stop because `je` – Iłya Bursov Oct 14 '14 at 19:33
1

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"

User.1
  • 2,562
  • 3
  • 33
  • 40
  • Good, but you don't need CX as a separate loop counter when you already have BX counting down to zero. Use `jge` after `dec bx` to re-enter the loop as long as `BX` is non-negative. [The `loop` instruction is slow](https://stackoverflow.com/questions/35742570/why-is-the-loop-instruction-slow-couldnt-intel-have-implemented-it-efficiently) and should only be used when optimizing for code-size. (And then only when you can't do the same thing with less code size using a JCC) – Peter Cordes Dec 25 '17 at 05:53
0

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
rashedcs
  • 3,588
  • 2
  • 39
  • 40
  • This has a bunch of wasted instructions inside loops. e.g. You don't need `CX` as a separate counter when you already have `BX`. – Peter Cordes Dec 25 '17 at 04:57
  • You asked on a now-deleted answer how to write better answers: 1. Explain in English what the answer to the question is, and the point of any code in your answer. 2. Comment your code. It's *much* more useful to future readers to see why your code is doing what it's doing. For example, see this answer about how to use `div`: https://stackoverflow.com/questions/38416593/why-should-edx-be-0-before-using-the-div-instruction/38416896#38416896. Or this answer about printing integers: https://stackoverflow.com/a/46301894/224132. – Peter Cordes Dec 25 '17 at 05:51
  • Also, some of my highly-voted answers are pretty good IMO, but few of them are answering debugging questions so the format is different. – Peter Cordes Dec 25 '17 at 05:56
  • Tnq @PeterCordes. I am oblige to you. – rashedcs Dec 25 '17 at 07:20