Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Assembly language: Why wouldnt my code work the way i want it to?

  1. Feb 21, 2009 #1
    this is my code to convert lower case strings to upper case.
    i want to ask the user whether to continue or not. but no matter what i type, the prog keeps stoping. try to copy paste this code to ur emulator and u'll know what im saying.

    also, how should i replace the include 'emu8086.inc' with??
    .
    .MODEL SMALL

    .STACK 100H

    .DATA

    MSG1 DB 0DH,0AH,"ENTER A LOWER CASE LETTER: $"
    MSG2 DB 0DH,0AH, "IN UPPER CASE IT IS:"
    CHAR DB ?, "$"

    .CODE
    MAIN PROC
    include 'emu8086.inc'

    MOV AX, @DATA
    MOV DS, AX

    label3:
    LEA DX, MSG1


    MOV AH, 9
    INT 21H

    MOV AH, 1
    INT 21H

    CMP AL, 'z'
    JG label1

    CMP AL, 'a'
    JL label2

    SUB AL, 20H

    MOV CHAR, AL

    LEA DX, MSG2

    MOV AH,9
    INT 21H

    JMP label4


    label1:
    PRINT ' Please enter a lower case alphabet for conversion!'
    jmp label3

    label2:
    PRINT ' Please enter a lower case alphabet for conversion!'
    jmp label3

    label4:
    PRINT ' Do you want to continue?<y/n>'
    MOV AH, 1
    INT 21H

    CMP AL, 'n'
    int 20h

    CMP AL, 'y'
    jmp label3

    exit:
    MAIN ENDP
    END MAIN
     
  2. jcsd
  3. Feb 21, 2009 #2

    rcgldr

    User Avatar
    Homework Helper

    The problem is here:

    CMP AL, 'n'
    int 20h

    You do a compare, but then exit (int 20h) no matter what. You need a conditional branch after a compare.


    I don't know what emu8086.inc does, are you running on a non-intel cpu?

    If you're using MASM, (Microsoft's assembler), it defaults to 8086 unless you specify a cpu, such as .186 or .686.
     
    Last edited: Feb 21, 2009
  4. Feb 22, 2009 #3
    i see. so how i should i do a conditional branch? can you show it to me?


    and i am using the 8086 emulator. if i take that out, there will be a warning message that says my parameter is wrong. i copied that line from the example given. er, how should i fix that?
     
  5. Feb 22, 2009 #4
    Well, CMP does a logical subtraction between source and destination, so you should use:

    jz: jump if zero
    je: jump if equal

    jne: jump if not equal
    jnz: jump if not zero

    jg: jump if greater than
    jge: jump if greater than or equal to

    jl: jump if less than
    jle: jump if less than or equal to

    I would recommend using these, and not the ones for unsigned arithmetic, since the logical subtraction is involved (suggestion).

    For example:

    cmp al, 'n'
    je equalToNBranch
    jmp notEqualToNBranch

    equalToNBranch: ; code for the case al == 'n'
    jmp done;

    notEqualToNBranch: ; code for the case al =/= 'n'
    jmp done;

    done: ; further code...

    Not the most efficient usage, but hopefully clear enough to give you the idea.
     
  6. Feb 22, 2009 #5
    thanks, it works and i also mod-ed it to work so that when the user enter something other than y/n, it will keep asking the user to enter y/n again. but how do i make them go to a new line everytime?

    label4:
    PRINT ' Do you want to continue?<y/n>'
    MOV AH, 1
    INT 21H

    CMP AL, 'n'
    JE label6
    JNE label7

    label7:
    CMP AL, 'y'
    JE label3
    JNE label4

    label6:
    int 20h
     
  7. Feb 22, 2009 #6

    rcgldr

    User Avatar
    Homework Helper

    You left out the 0dh and 0ah in the print statements. Try 0dh, 0ah, ' ...' to see if that's allowed.
     
  8. Feb 22, 2009 #7
    i know 0dh and 0ah. but that cant work.

    but the more important question i have now is, how do i 'declare' or 'pass' the parameter?

    in my code, i used something like: include emu8086:

    which, i copied from the example. if i dont do that, the "PRINT" wouldnt work. i know it's bad programming but i didnt know what to do. so what should i do to replace that line (include emu8086)?

    anyway, this is my code:

    .
    .MODEL SMALL

    .STACK 100H

    .DATA

    MSG1 DB 0DH,0AH,"ENTER A LOWER CASE LETTER: $"
    MSG2 DB 0DH,0AH, "IN UPPER CASE IT IS:"
    CHAR DB ?, "$"

    .CODE
    MAIN PROC
    include 'emu8086.inc'

    MOV AX, @DATA
    MOV DS, AX

    label3:
    LEA DX, MSG1


    MOV AH, 9
    INT 21H

    MOV AH, 1
    INT 21H

    CMP AL, 'z'
    JG label1

    CMP AL, 'a'
    JL label2

    SUB AL, 20H

    MOV CHAR, AL

    LEA DX, MSG2

    MOV AH,9
    INT 21H

    JMP label4


    label1:
    PRINT ' Please enter a lower case alphabet for conversion!'
    jmp label3

    label2:
    PRINT ' Please enter a lower case alphabet for conversion!'
    jmp label3

    label4:
    PRINT ' Do you want to continue?<y/n>'
    MOV AH, 1
    INT 21H

    CMP AL, 'n'
    JE label6
    JNE label7

    label7:
    CMP AL, 'y'
    JE label3
    JNE label4

    label6:
    int 20h

    exit:
    MAIN ENDP
    END MAIN
     
  9. Feb 23, 2009 #8
    sorry again. it's all good now. thank you, Jeff and csprof2000! have a nice day! =)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Assembly language: Why wouldnt my code work the way i want it to?
  1. Assembly code (Replies: 4)

  2. Assembly language (Replies: 5)

Loading...