1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
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 homework help

  1. Jun 23, 2012 #1
    1. The problem statement, all variables and given/known data
    I'm having hard time appreciating this topic since they're not discussed well and, as of now, I still don't have resources or sample programs.
    We're required to display Az By Cx . . . Yb Za including spaces using DOS debug.

    2. Relevant equations
    3. The attempt at a solution
    My code displays only AABBCCDDEE..XXYYZZ. We already have a program for displaying Aa Bb Cc ... Xx Yy Zz so I attempted to revise that code. But I'm so unfortunate.
    Code (Text):
    :0100 mov ah,02
    :0102 mov dl,41
    :0104 int 21
    :0106 push dx
    :0107 mov cl,7a
    :0109 int 21
    :010B push dx
    :010C inc dl
    :010E dec cl
    :0110 cmp dl,5b
    :0113 jne 104
    :0115 int 20
    Am I doing it correctly or I'm mixing up different instructions?
     
  2. jcsd
  3. Jun 23, 2012 #2

    rcgldr

    User Avatar
    Homework Helper

    The display character routine you're using (AH = 02, INT 21H), uses dl, not cl, so you'll need to figure out how to use dl for both the upper and lower case output. The program does push dx twice in the loop, but it nevers does a pop .... You probably don't need to use push ... you could just use xchg cl,dl , and clean up the code you already have. Most of the registers are saved (except for AL and maybe AH) with those INT 21 calls.
     
    Last edited: Jun 23, 2012
  4. Jun 23, 2012 #3

    Mark44

    Staff: Mentor

    Also, I think you're not looping back to the right line. Near the end you have jne 104, which transfers control back to memory address 104. The instruction at that address is int 21. When you use the DOS interrupt (int 21h), you need to set up the registers appropriately, which doesn't seem to be happening here.
     
  5. Jun 23, 2012 #4

    Mark44

    Staff: Mentor

    It's helpful to have an example program that functions close to what you need for this problem, but you should also be thinking about your algorithm, which is something like this.
    Code (Text):

    count = 26
    firstChar = 0x41 ('A')
    lastChar = 0x7A ('z')
    loop
      print firstChar
      print lastChar
      print newline
      increment firstChar
      decrement lastChar
      decrement count
    until loop == 0  
     
     
  6. Jun 23, 2012 #5
    Mastur,

    You should tell us for what CPU that assembly is written.

    Ratch
     
  7. Jun 23, 2012 #6
    Intel.

    As of now, I was able to display only AzB (no space yet). I'll try it again later.
     
  8. Jun 24, 2012 #7

    Mark44

    Staff: Mentor

    It looks like Intel 8086 assembly with the int 21h DOS instructions, which is confirmed by the OP.

     
  9. Jun 24, 2012 #8
    Sorry if I cannot fully understand what you are saying. Anyway, I made another progress. Now it displays "A zByzCyzDyzEyzFyzGyzHyzIyzJyzKyzLyzMyzNyzOyzPyzQyzRyzSyzTyzUyzVyzWyzXyzYyzZyz". I just don't know why a space exist after A.

    1387:0100 mov ah,02
    1387:0102 mov dl,41
    1387:0104 int 21
    1387:0106 push dx
    1387:0107 inc dl
    1387:0109 xchg cl,dl
    1387:010B int 21
    1387:010D mov dl,7a
    1387:010F int 21
    1387:0111 dec dl
    1387:0113 xchg dl,cl
    1387:0115 cmp dl,5b
    1387:0118 jne 104
    1387:011A int 20

    zy doesn't seem decrementing. Did I place an increment instruction right after the decrement?

    Btw, anyone of you have sample codes for assembly language? I am using DOS debug. I can easily understand codes in a program rather than wordy descriptions.
     
  10. Jun 24, 2012 #9

    rcgldr

    User Avatar
    Homework Helper

    Since you're trying, a bit more help this time. I'll get you started, see if you can finish this:

    1387:0100 mov dl,41
    1387:0102 mov cl,7a
    1387:0104 mov ah,02
    1387:0106 int 21
    1387:0108 xchg cl,dl
    1387:010A int 21
    1387:010C xchg cl,dl
    1387:010E inc dl
    1387:0110 dec cl
    ...

    As far as an assembler goes, I'm not sure where to get an old DOS compatable development set, such as Masm 6.11 and Codeview (a source level debugger). You could download and use Microsoft Visual C++ express, which includes Masm 7.0, but creating a new project is a bit complicated. You create an empty project, add the assembly code (.asm text file) as an exsiting item to the project, then you have to specify a custom build step to do the assembly. I can explain more details on this if you want to try this.
     
    Last edited: Jun 24, 2012
  11. Jun 24, 2012 #10
    WOW. Thank you very much for your help. I was able to do it. The code seems shorter than what I've been doing before.

    Though, I have some questions regarding the code you've given.

    1. xchg cl,dl means exchange the values of cl and dl, right? Why are there two xchg instructions? Exchanging them twice will just return them to their original places, right?

    2. Why didn't you use push instruction?

    It might sound stupid but I really want to know about this. I want to appreciate Assembly Language more than or at least the same as how I appreciated C# and C++. I'm google-ing it but it is not giving me the answer I wanted.

    I want to know how to do that on C++, but I'm afraid I do not have enough time to focus on it, as you say, its bit complicated. It is because we also have our thesis to be accomplished.

    Anyway, in-case I have enough time already, I'll be PMing you to know more about that. Again, thank you very much.

    Edit: Since it is required to have spaces every after the two letters, hopefully I can do this now without asking for help!

    Edit2: I was able to do it. I used push and pop even if I don't understand why.
     
    Last edited: Jun 24, 2012
  12. Jun 24, 2012 #11

    Mark44

    Staff: Mentor

    In the algorithm in wrote in post #4, add a command to print a space (0x20) after you print the second character and before you print the newline character. Presumably you already figured this out.

    As far as the push and pop are concerned, the reason for using them is to be able to use a given register without permanently losing its value.

    For example, if cx contains 20 and you need to use cx for something, you could do this:
    Code (Text):

    push cx ; store CX value on stack
    move cl, 7a
    ; do some stuff
    pop cx ; restore save value from stack
     
    Generally, each push instruction should have a pop instruction fairly close by, to leave the stack in the same in the same state it was in before the push.
     
  13. Jun 24, 2012 #12

    rcgldr

    User Avatar
    Homework Helper

    For this particular program, you don't have to use push and pop, just keep the display values in registers other than dl. INT 21 generally saves the registers (it does the pushes and pops for you), except for AL, which INT 21 normally uses for a return value.

    Code (Text):

    1387:0100 mov ah,02
    1387:0102 mov ch,41
    1387:0104 mov cl,7a
    1387:0106 mov dl,ch
    1387:0108 int 21
    1387:010A mov dl,cl
    1387:010C int 21
    1387:010E mov dl,20
    1387:0110 int 21
    1387:0112 inc ch
    1387:0114 dec cl
    1387:0116 cmp ch,5b
    1387:0119 jne 0106
    1387:011B int 20
     
    If or when you get to using a proper assembler, masm 5.0 style version of this program:

    Code (Text):

    cseg    segment
            assume  cs:cseg,ds:cseg,es:cseg,ss:nothing
            org     0100h
    disp0:  mov     ah,02h
            mov     ch,41h
            mov     cl,7ah
    disp1:  mov     dl,ch
            int     21h
            mov     dl,cl
            int     21h
            mov     dl,20h
            int     21h
            inc     ch
            dec     cl
            cmp     ch,5bh
            jne     disp1
            int     20h
    cseg    ends
            end     disp0
     
    masm 6.11 (or later) style version of code:
    Code (Text):

            .model  tiny,c
            .8086
            .code
            assume  cs:@code,ds:@code,es:@code,ss:nothing
            org     0100h
    disp    proc    far
            mov     ah,02h
            mov     ch,41h
            mov     cl,7ah
    disp1:  mov     dl,ch
            int     21h
            mov     dl,cl
            int     21h
            mov     dl,20h
            int     21h
            inc     ch
            dec     cl
            cmp     ch,5bh
            jne     disp1
            int     20h
    disp    endp
            end     disp
     
     
    Last edited: Jun 24, 2012
  14. Jun 24, 2012 #13
    I thought it can only hold one value. Or this two instructions are the same as xchg dl,cl?

    And other than those you have used, what are the other registers that I can use? Is it like variable in high-level programming languages as long as I use only two letters?
     
  15. Jun 24, 2012 #14

    rcgldr

    User Avatar
    Homework Helper

    Yes, registers are like variables (except they are in the cpu instead of ram).

    Registers used as 16 bit values:

    ax
    bx
    cx
    dx
    si
    di
    bp (normally used as backup for stack pointer)
    sp (stack pointer)

    Some of the register halves can be used for 8 bit values:

    ah
    al
    bh
    bl
    ch
    cl
    dh
    dl

    wiki articles:

    wiki_x86.htm

    wiki_x86_instructions.htm
     
    Last edited: Jun 25, 2012
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Assembly Language homework help
  1. Assembly Language (Replies: 0)

Loading...