x86 assembly output string

Discussion in 'Programmer's Corner' started by adrenalina, Nov 2, 2012.

  1. adrenalina

    Thread Starter Active Member

    Jan 4, 2011
    78
    3
    Hello everybody. I ran into a problem programming x86 assembly and was hoping that you guy could help figure out what is wrong. The problem is with the code below. It should output W12 to the console when the enter key is pressed, very simple. The problem is that it outputs garbage. It outputs random characters and the program finishes.

    Code ( (Unknown Language)):
    1.  
    2. data    SEGMENT
    3.  
    4. key db 0
    5. msg db "W12$"
    6. data    ENDS
    7.  
    8.  
    9. code    SEGMENT
    10.     ASSUME CS: code, DS: data
    11. mov ax, SEG data
    12. mov ds, ax
    13.  
    14. start:
    15. mov     ah, 8       ; read key from keyboard
    16. int 21h
    17. mov     key, al
    18. cmp key, 13
    19. JNZ start       ; if key is not carriage return go to start
    20.  
    21. mov     ah, 9
    22. mov dx, OFFSET msg
    23. int 21h
    24.  
    25. mov ah, 04ch
    26. int 21h
    27.  
    28. code    ENDS
    29.  
    30.     END
    31.  
    I tried using the code below. Which is very similar just a few changes and it worked perfectly fine.

    Code ( (Unknown Language)):
    1.  
    2. .model small
    3. .data
    4. key     db 0
    5. msg db "W12$"
    6.  
    7. .code
    8. mov ax, SEG @data
    9. mov ds, ax
    10.  
    11. start:
    12. mov     ah, 8       ; read key from keyboard
    13. int 21h
    14. mov     key, al
    15. cmp key, 13
    16. JNZ start       ; if key is not carriage return go to start
    17.  
    18. mov     ah, 9
    19. mov dx, OFFSET msg
    20. int 21h
    21.  
    22. mov ah, 04ch
    23. int 21h
    24.  
    25.     END
    26.  
    The first code is the way the professor taught us to structure the code(using SEGMENT, ENDS, etc.), so I am trying to make that work and the second is something I tried by looking up information on the internet.
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    I guess the root of your problem. Is that DS:DX is not pointing to the string msg in the first example. I suspect DS is not set up correctly. Problems like this call for using the debugger. Have your professor talked about this yet. In assembler programming it is VERY VERY important that you master using the debugger. Which assembler tool do you use?
     
  3. nigelwright7557

    Senior Member

    May 10, 2008
    487
    71
    Interesting to see people still using assembler.
    I last used it 15-20 years ago on the PC partly because there was little choice.
    I wrote a 300,000 line program for designing printed circuit boards.

    I think since about Vista you can no longer run DOS programs under Windows.
    It works ok with XP though.

    I guess its like electronic engineers still learning about valves despite it being highly unlikely they will use them now.
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,363
    I guess that is a wrong guess. Both ASM and vacuum tubes are still in use today. If you want to be a well rounded engineer, you should have those tools under your belt.
     
  5. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,363
    How about:

    Code ( (Unknown Language)):
    1.  
    2.    mov  ax, SEG @data
    3.  
     
  6. adrenalina

    Thread Starter Active Member

    Jan 4, 2011
    78
    3
    no, my professor hasn't talked about a debugger. We are using MASM 6.11. What debugger could I use?
    I tried that mrchips, but the assembler gave me an error.
    error A2006: undefined symbol.
     
  7. adrenalina

    Thread Starter Active Member

    Jan 4, 2011
    78
    3
    Problem fixed! At the end it should say end start(or the name of the label where the program starts)

    Code ( (Unknown Language)):
    1.  
    2.    
    3. data    SEGMENT
    4.  
    5. key db 0
    6. msg db "W12$"
    7. data    ENDS
    8.  
    9.  
    10. code    SEGMENT
    11.     ASSUME CS: code, DS: data
    12. start:
    13. mov ax, SEG data
    14. mov ds, ax
    15.  
    16. mov     ah, 8       ; read key from keyboard
    17. int 21h
    18. mov     key, al
    19. cmp key, 13
    20. JNZ start1      ; if key is not carriage return go to loop
    21.  
    22. mov     ah, 9
    23. mov dx, OFFSET msg
    24. int 21h
    25.  
    26. mov ah, 04ch
    27. int 21h
    28.  
    29. code    ENDS
    30.  
    31.     END start
    32.  
     
Loading...