1

It needs to allow a two-digit number for input that will be used to indicate how many times the name is printed. I can't figure out how to separate the second digit though and have it checked to make sure it is between 0x30 and 0x39. I also keep getting this weird box after the name that has 0017 inside it.

    .data   
    input_msg_len:  .long 26
    input_msg:  .ascii "Enter a two-digit number: "
    name:       .ascii "Michael Chabon\n"
    name_len:   .long 16
    max:        .long 0
    count:      .long 0
    tmp:        .long 0
    input_str:  .ascii "??" 

    .text               
    .global _start          
    _start:     
        mov $4, %eax    
        mov $1, %ebx
        mov $input_msg, %ecx
        mov input_msg_len, %edx
        int $0x80

        mov $3, %eax    
        mov $0, %ebx    
        mov $input_str, %ecx 
        mov $2, %edx    
        int $0x80   

        mov $input_str, %eax
        add count, %eax

            mov $input_str, %eax 
        mov (%eax), %bl 
        cmp $0x30, %bl  
        jl  _start      
        cmp $0x39, %bl  
        jg  _start  

        mov count, %eax 
        inc %eax        
        mov %eax, count 

        sub $0x30, %bl
        mov %bl, max

        mov $10, %bl    
        imul    %bl
        mov %bl, max

#Not sure how to check second char in input_str.
#Want to check it then subtract $0x30 and move to tmp before adding tmp to max.

        mov $0, %edi    
    again:
        cmp max, %edi   
        je  end     

        mov $4, %eax    
        mov $1, %ebx    
        mov $name, %ecx
        mov name_len, %edx
        int $0x80       

        inc %edi        
jmp again       


    end:
        mov $1, %eax    
        int $0x80       

Thanks in advance!

7
  • I'm assuming you're using int 80h system calls on Linux? Also, I'm assuming you're using GAS? Mar 9, 2013 at 23:54
  • I think so, though honestly the professor never told us specifics. He kind of just handed us some programs and said "Now do this" without explaining to us anything except that it was an assembly language. :/ But int 80h and GAS seem right.
    – Cat
    Mar 10, 2013 at 0:03
  • Sounds like a terrible professor. Anyway, I would write little functions for each system call that you're making. Mar 10, 2013 at 0:06
  • Is there a way to do it without functions? He doesn't want us to use those at the moment.
    – Cat
    Mar 10, 2013 at 0:09
  • Well understanding the code you have in front of you would be a good start. I don't know all of the linux syscall numbers off the top of my head, so I'm assuming you don't either. Mar 10, 2013 at 0:14

1 Answer 1

0

There are some bugs in your code.

Below, 2 first lines of that block are redundant, as mov $input_str, %eax overwrites eax anyway.

    mov $input_str, %eax
    add count, %eax

    mov $input_str, %eax

Then here, it makes no sense to load count into eax here:

    mov count, %eax 
    inc %eax        
    mov %eax, count

You can do this in a lot shorter and clearer way with:

    incl count

Then, next bug is that you recently loaded count into eax, and then multiply the lowest 8 bits of count loaded into al with 10, in this piece of code:

    mov (%eax), %bl  // bl = first character
    cmp $0x30, %bl  
    jl  _start      
    cmp $0x39, %bl  
    jg  _start  

    mov count, %eax // eax = count
    inc %eax        // eax++
    mov %eax, count // count = eax

    sub $0x30, %bl  // 0 <= bl <= 9
    mov %bl, max    // max = bl <- you lose this value in the next mov %bl, max

    mov $10, %bl    // bl = 10
    imul    %bl     // ax = 10 * and(count, 0xff) // ax = al*bl (signed multiply)
    mov %bl, max    // max = 10 <- here you overwrite the value of max with 10

So, according to my intuition you don't want to do ax = 10 * and(count, 0xff), but 10 * (first number). imul %bl does o signed multiply between al and bl, and stores the result in ax. So the code above could be changed to something like this:

    mov (%eax), %bl  // bl = first character
    cmp $0x30, %bl  
    jl  _start      
    cmp $0x39, %bl  
    jg  _start  

    incl count

    pushl %eax      // push eax to stack

    sub $0x30, %bl  // 0 <= bl <= 9
    mov $10, %al    // al = 10
    imul     %bl    // ax = 10 * bl (signed multiply)
    mov %al, max    // 0 <= max <= 90

Then, you can check the second character similarly to the first character:

    pop %eax        // pop eax from stack

    incl %eax

    mov (%eax), %bl  // bl = second character
    cmp $0x30, %bl  
    jl  _start      
    cmp $0x39, %bl  
    jg  _start

    sub $0x30, %bl  // 0 <= bl <= 9

    add %bl, max    // 0 <= max <= 99

I strongly recommend you to learn to use some debugger. gdb has several frontends, of which I think ddd works best according to my experience. gdbtui is also convenient.

1
  • Thanks so much! That was a lot of help! :)
    – Cat
    Mar 10, 2013 at 3:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.