Results 1 to 3 of 3

Thread: MASM Help.

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2005
    Location
    Usa Nyc
    Posts
    45

    MASM Help.

    I'm a VB and a C++ programmer and just started to learn asm. I'm trying to replicate my old c++ newbie programs into asm code. One of the programs i tried to replicate is an easy one it just draws a right triangle in console mode with asterisks.
    Asm Code:
    Code:
    main proc
    
        LOCAL x:DWORD
        LOCAL y:DWORD
        LOCAL inptVal:DWORD
    
        mov eax, sval(input("Height of the triangle:"))
        mov y,0
    
        YLoop:
            print chr$(13,10)
            mov x,0
            add y,1
            mov ebx,y
            cmp y,eax
            jg Done
            XLoop:
                add x,1
                cmp x,ebx
                jg YLoop
                print "*"
                jmp XLoop
    
        Done:
        mov inptVal, input("Press any key to continue . . .") 
        ;to halt and let the output be seen
    
        ret
    
    main endp
    For some reason it just does 2 lines of asterisks.
    Last edited by Evgeni; Feb 2nd, 2005 at 03:37 PM.

  2. #2
    Hyperactive Member Maven's Avatar
    Join Date
    Feb 2003
    Location
    Greeneville, TN
    Posts
    322

    Re: MASM Help.

    Quote Originally Posted by Evgeni
    I'm a VB and a C++ programmer and just started to learn asm. I'm trying to replicate my old c++ newbie programs into asm code. One of the programs i tried to replicate is an easy one it just draws a right triangle in console mode with asterisks.
    Asm Code:
    Code:
    main proc
    
        LOCAL x:DWORD
        LOCAL y:DWORD
        LOCAL inptVal:DWORD
    
        mov eax, sval(input("Height of the triangle:"))
        mov y,0
    
        YLoop:
            print chr$(13,10)
            mov x,0
            add y,1
            mov ebx,y
            cmp y,eax
            jg Done
            XLoop:
                add x,1
                cmp x,ebx
                jg YLoop
                print "*"
                jmp XLoop
    
        Done:
        mov inptVal, input("Press any key to continue . . .") 
        ;to halt and let the output be seen
    
        ret
    
    main endp
    For some reason it just does 2 lines of asterisks.
    You may want to start commenting you're code more hehe. ASM can be extremely hard to read if it's not commented. If this algorithm would have been a more complex one, I wouldn't have been able to help you.

    The first thing I see is that you're using EBX without saving it. That's a big no no in windows. The only registers you can use without saving them are eax, ecx, and edx. If you want to use the other registers you must push them onto the stack first and pop them off once you're finished using them.

    The reason you are seeing 2 lines of asterisks is because of the print statement you are using. That macros makes use of the eax register which is what you are using to store the height of you're triangle.

    push ebx

    mov eax, sval(input("Height of the triangle:"))
    mov y,0

    YLoop:
    push eax
    print chr$(13,10)
    pop eax
    mov x,0
    add y,1
    mov ebx,y
    cmp y,eax
    jg Done
    XLoop:
    add x,1
    cmp x,ebx
    jg YLoop
    push eax
    print "*"
    pop eax
    jmp XLoop

    Done:
    mov inptVal, input("Press any key to continue . . .")
    ;to halt and let the output be seen

    pop ebx
    You need to re-write this code and not use the ugly push and pop btw. You may also want to get rid of the variables and just use registers =)

    xor eax, eax is a good way to set a register to 0.

    peace!
    Education is an admirable thing, but it is well to remember from time to time that nothing that is worth knowing can be taught. - Oscar Wilde

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2005
    Location
    Usa Nyc
    Posts
    45

    Re: MASM Help.

    Thanks dude program works well. =D

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width