Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: HELP!! Assembly code

  1. #1
    Join Date
    Sep 2012
    Beans
    10

    Red face HELP!! Assembly code

    So, i was coding in assembly (IAS-32), when i got an arithmetical error. I searched over and over but couldnt figure it out. I dont know why but when I try to do 2/1, this error pops out. The problem is, I have to finish it today, so... any help will be good =D
    Here is the code:

    Code:
    .section .data
        mat:
            .int 1,3,7,2
    
    .section .bss
        .lcomm mat1, 40
    
    .section .text
    
    
    .globl _start
    _start:
        nop
        movl $0, %ecx
    
        calcDet:
        movl mat(,%ecx,4), %eax
        add $2, %ecx
        movl mat(,%ecx,4), %ebx
        mul %ebx
        cmp $3, %ecx
        je inverte
        pushl %eax
        dec %ecx
        jmp calcDet
        
        inverte:
        pop %ebx
        sub %eax, %ebx
        movl $0, %ecx
    
        diagPrin:
        movl mat(,%ecx,4), %eax
        cmp $0, %eax
        je gamb3
        div %ebx
        
        gamb3:
        cmp $2, %ecx
        je executa
        add $2, %ecx
        movl %eax, mat1(,%ecx,4)
        jmp diagPrin
    
        executa:
        sub $2, %ecx
        movl %eax, mat1(,%ecx,4)
        inc %ecx
    
        inverteSinal:
        movl mat(,%ecx,4), %eax
        cmp $0, %eax    
        je gamb
        div %ebx
        
        gamb:
        movl $-1, %edx
        mul %edx
        movl %eax, mat1(,%ecx,4) 
        add $2, %ecx
        movl mat(,%ecx,4), %eax
        cmp $0, %eax
        je gamb2
        div %ebx   <-----------------------------ERROR
    
        gamb2:
        mul %edx
        movl %eax, mat1(,%ecx,4)
        
        
    fim:
        movl $1, %eax
        movl $0, %ebx
        int $0x80
    PS: This code tries to do the inverse of the matrix {1,3,7,2}
    l 1 3 l
    l 2 7 l
    Last edited by YuKill; September 27th, 2012 at 03:36 PM.

  2. #2
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: HELP!! Assembly code

    Did you try debugging your program. My guess is that ebx has a value of 0 at the time of execution, so you are dividing by 0. By step-by-step executing your program, you will see where such a value comes from.

  3. #3
    Join Date
    Sep 2012
    Beans
    10

    Re: HELP!! Assembly code

    I thought that so.... but when i ran gdb, it comes out that ebx is 1

    (gdb)
    60 add $2, %ecx
    (gdb)
    61 movl mat(,%ecx,4), %eax
    (gdb)
    62 cmp $0, %eax
    (gdb)
    63 je gamb2
    (gdb)
    64 div %ebx
    (gdb) p $ebx
    $1 = 1

  4. #4
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,165
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: HELP!! Assembly code

    Quote Originally Posted by YuKill View Post
    I thought that so.... but when i ran gdb, it comes out that ebx is 1

    (gdb)
    60 add $2, %ecx
    (gdb)
    61 movl mat(,%ecx,4), %eax
    (gdb)
    62 cmp $0, %eax
    (gdb)
    63 je gamb2
    (gdb)
    64 div %ebx
    (gdb) p $ebx
    $1 = 1
    Very hazy memory, so I'm shooting in the dark, but since you are likely using signed numbers, shouldn't you be using IDIV (and IMUL) instead of DIV? Maybe the complaint is about EDX/EAX contents, btw.

  5. #5
    Join Date
    Jun 2010
    Beans
    92

    Re: HELP!! Assembly code

    Are you running the code on a PC as a user logged into an O.S. such as Ubuntu or Windows? If so, the x86-style CPU is running in 'Protected Mode' and your code is running in 'Userland'.

    What you're trying to do requires direct access to the hardware and modern O.S.'s try their best to prevent that from happening from Userland.

    A simple way to get direct access to your PC's hardware in order to play with assembly code is to boot a 'Real-Mode' O.S. such as DOS (and get a DOS assembler, linker, etc).

    Or write kernel-level code (drivers, etc).
    Last edited by xb12x; September 27th, 2012 at 10:08 PM.

  6. #6
    Join Date
    Sep 2012
    Beans
    10

    Re: HELP!! Assembly code

    Well, I'm running at Ubuntu. I wouldn't get this error if I couldn't access the hardware of my computer, would I? So, I dont think the code is wrong... if I change the vector(mat) to 1,0,1,0 it works just fine...

    Still lossst....

  7. #7

    Re: HELP!! Assembly code

    Quote Originally Posted by YuKill View Post
    Well, I'm running at Ubuntu. I wouldn't get this error if I couldn't access the hardware of my computer, would I?
    Quote Originally Posted by RinkWorks.com
    A man attempting to set up his new printer called the printer's tech support number, complaining about the error message: "Can't find the printer." On the phone, the man said he even held the printer up in front of the screen, but the computer still couldn't find it.
    Analogous mistake. You as a computer user may have access to the hardware, but you as a programmer still can't necessarily "access" the hardware. There are protection mechanisms in place to prevent (unelevated) direct hardware access, which is what xb12x is talking about.

    However, I'm not sure that's your real problem. My x86 really isn't up to snuff anymore (it never really was), but...

    Am I mistaken in guessing that when you ran in the debugger, it didn't cause the error? You didn't show that part of the debugging session. If that's the case, you probably have some very subtle bug in another part of your program. Assembly, man, it's awful for that.

  8. #8
    Join Date
    Sep 2012
    Beans
    10

    Re: HELP!! Assembly code

    Nope, in the debugger I get the same error... =/

  9. #9
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: HELP!! Assembly code

    Quote Originally Posted by YuKill View Post
    Nope, in the debugger I get the same error... =/
    Ok, then run your program step-by-step until you hit the error and post us the output. Your GDB log is too short - it ends before the error occurs. Show us all the register contents right one command before the error.

  10. #10
    Join Date
    Sep 2012
    Beans
    10

    Red face Re: HELP!! Assembly code

    Actually, if a I press enter one more time, the error pops out which means that it doesnt execute the division or it simply (but not as simply as it seems) doesnt do that! D=

    13 nop
    (gdb) n
    14 movl $0, %ecx
    (gdb)
    17 movl mat(,%ecx,4), %eax
    (gdb)
    18 add $2, %ecx
    (gdb)
    19 movl mat(,%ecx,4), %ebx
    (gdb)
    20 mul %ebx
    (gdb)
    21 cmp $3, %ecx
    (gdb)
    22 je inverte
    (gdb)
    23 pushl %eax
    (gdb)
    24 dec %ecx
    (gdb)
    25 jmp calcDet
    (gdb)
    17 movl mat(,%ecx,4), %eax
    (gdb)
    18 add $2, %ecx
    (gdb)
    19 movl mat(,%ecx,4), %ebx
    (gdb)
    20 mul %ebx
    (gdb)
    21 cmp $3, %ecx
    (gdb)
    22 je inverte
    (gdb)
    28 pop %ebx
    (gdb)
    29 sub %eax, %ebx
    (gdb)
    30 movl $0, %ecx
    (gdb)
    33 movl mat(,%ecx,4), %eax
    (gdb)
    34 cmp $0, %eax
    (gdb)
    35 je gamb3
    (gdb)
    36 div %ebx
    (gdb)
    39 cmp $2, %ecx
    (gdb)
    40 je executa
    (gdb)
    41 add $2, %ecx
    (gdb)
    42 movl %eax, mat1(,%ecx,4)
    (gdb)
    43 jmp diagPrin
    (gdb)
    33 movl mat(,%ecx,4), %eax
    (gdb)
    34 cmp $0, %eax
    (gdb)
    35 je gamb3
    (gdb)
    36 div %ebx
    (gdb)
    39 cmp $2, %ecx
    (gdb)
    40 je executa
    (gdb)
    46 sub $2, %ecx
    (gdb)
    47 movl %eax, mat1(,%ecx,4)
    (gdb)
    48 inc %ecx
    (gdb)
    51 movl mat(,%ecx,4), %eax
    (gdb)
    52 cmp $0, %eax
    (gdb)
    53 je gamb
    (gdb)
    54 div %ebx
    (gdb)
    57 movl $-1, %edx
    (gdb)
    58 mul %edx
    (gdb)
    59 movl %eax, mat1(,%ecx,4)
    (gdb)
    60 add $2, %ecx
    (gdb)
    61 movl mat(,%ecx,4), %eax
    (gdb)
    62 cmp $0, %eax
    (gdb)
    63 je gamb2
    (gdb)
    64 div %ebx
    (gdb)

    Program received signal SIGFPE, Arithmetic exception.
    gamb () at InvMatriz.s:64
    64 div %ebx

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

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