Jump to content


Check out our Community Blogs

restin84

Member Since 04 Oct 2008
Offline Last Active Sep 13 2013 07:58 AM
-----

Posts I've Made

In Topic: [SOLVED]Should I place local variables in registers before performing operati...

01 November 2012 - 08:04 PM

So since I replaced
movl -8(%ebp), %ebx 
with
movl -8(%ebp), %ecx 
If I had used EBX instead then prior to...
L1: pushl A(,%ecx,4)
I should have said
pushl	    %ebx
and after
addl $12, %esp
I should have
popl %ebx
Is this right? But I guess I should use EBX only if I HAVE TO then?

In Topic: [SOLVED]Should I place local variables in registers before performing operati...

01 November 2012 - 07:39 PM

1) loop is an instruction, don't use it as a label name. Pick something else.

2) You can't use EBX like that. According to conventions you have to save EBX, ESI, EDI, and EBP before using them in your function, and you must restore their values before returning. You can freely use EAX, EDX, and ECX. You can't expect these registers to have the same values after you call a function.

As for efficiency, I'd fix these things first and then try optimizing it.

So I changed the label name for my loop and I used register EAX in place of register EBX. Do you have any code snippits of saving/restoring registers EBX,ESI, EDI, EBP? I can't seam to find any illustrations of this.
    .data
    .align 4
A:    .long 1,2,3,4,5

    .section .rodata
str:    .string "A[%d] = %d"
ebxcontents:    .string "ecx = %d \n"
eaxcontents:    .string "eax = %d\n"
    .text
.global main
main:
    pushl    %ebp
    movl    %esp, %ebp
    subl    $8, %esp
    movl    $5, -4(%ebp)    #n = 5
    movl    $0, -8(%ebp)    #i = 0

    
    movl    -4(%ebp), %eax    #%eax = n
    movl    -8(%ebp), %ecx  #%ecx = i
    cmpl    %eax, %ecx
    jge    done
L1:    pushl    A(,%ecx,4)
    pushl    %ecx
    pushl    $str
    call    printf
    addl    $12, %esp
    incl    -8(%ebp)
    movl    -4(%ebp), %eax    #%eax = n
    movl    -8(%ebp), %ecx  #%ebx = i
    cmpl    %eax, %ecx
    jl    L1    
done:    
    leave
    ret
  


In Topic: [SOLVED]Should I place local variables in registers before performing operati...

01 November 2012 - 05:52 PM

If they're used frequently, then yes; otherwise, don't worry too much about it. What do you mean specifically? Can you show us some code?

Okay I have this. Just sequentially printing elements of an array. I don't know if maybe I can make it cleaner?/more efficient?
.data
    .align 4
A:    .long 1,2,3,4,5

    .section .rodata
str:    .string "A[%d] = %d"
ebxcontents:    .string "ebx = %d \n"
eaxcontents:    .string "eax = %d\n"
    .text
.global main
main:
    pushl    %ebp
    movl    %esp, %ebp
    subl    $8, %esp
    movl    $5, -4(%ebp)    #n = 5
    movl    $0, -8(%ebp)    #i = 0

    
    movl    -4(%ebp), %eax    #%eax = n
    movl    -8(%ebp), %ebx  #%ebx = i
    cmpl    %eax, %ebx
    jge    done
loop:    pushl    A(,%ebx,4)
    pushl    %ebx
    pushl    $str
    call    printf
    addl    $12, %esp
    incl    -8(%ebp)
    movl    -4(%ebp), %eax    #%eax = n
    movl    -8(%ebp), %ebx  #%ebx = i
    cmpl    %eax, %ebx
    jl    loop    
done:    
    


    leave
    ret


In Topic: [SOLVED]Should I place local variables in registers before performing operati...

01 November 2012 - 05:32 PM

Well I don't have much yet. But I'm trying to avoid making too much mess. I'm new to assembly and I'm learning that it can get messy quickly. I have noticed that certain instructions such as cmpl don't take two memory instructions.

In Topic: [SOLVED]Why are library functions erasing the contents of registers?

30 October 2012 - 05:59 PM

Only certain registers are guaranteed to be preserved across function calls; any function can use the other registers as it likes. The registers that must be preserved are EDI, ESI, EBX, EBP, ESP. The functions you call can use EAX, ECX, and EDX without restoring them. In fact, functions put their return values in EAX.

Note: The list of registers is different for 64-bit systems.


So I have this so far
/************************************************************************/
/************************UNINITIALIZED-GLOBAL-DATA***********************/
    .comm A,100,4        #array A[]
   
/************************************************************************/
/****************************READ-ONLY-DATA******************************/
    .section    .rodata
    .align 4
str1:    .string    "Enter size of array: "
str2:    .string "Enter %d integer elements: "
frmt1:    .string    "%d"

   
teststr1:    .string "n=%d, i=%d"
teststr2:    .string    "n=%d\n"
teststr3:    .string "array size = %d"
teststr4:    .string "element = %d"
eaxcontents:    .string "%eax = %d"
ebxcontents:    .string "%ebx = %d"
contentsofesi:    .string "%esi = %d"
element1input:    .string "enter array element 1"
element2input:    .string "enter array element 2"
element1:    .string "element1 = %d"
element2:    .string "element2 = %d"
/************************************************************************/   
/****************************MAIN-FUNCTION*******************************/
    .text            #text section
.global    main            #declare main as global
main:
    pushl    %ebp        #prolog save old %ebp
    movl    %esp, %ebp    #prolog set new %ebp

    subl    $4, %esp    #create space in memory for local var n

    pushl    $str1        #push string onto stack for user input
    call    printf        #call printf
    addl    $4, %esp    #pop arguments off stack

    lea    -4(%ebp), %ebx  #load address for var n
    pushl    %ebx        #push %ebx holding address for n
    pushl    $frmt1        #push format string onto stack
    call    scanf        #call scanf
    addl    $8, %esp    #pop arguments off stack
    movl    -4(%ebp), %ebx

    pushl    %ebx
    pushl    $str2
    call    printf
    addl    $8, %esp
   
    movl    $0, %esi

    cmpl    %ebx, %esi
    jge    done
loop:    pushl    %edi
    pushl    $frmt1
    call    scanf
    addl    $8, %esp
    incl    %esi
    cmpl    %ebx, %esi
    jl    loop
done:   



    leave            #epilog
    ret            #epilog
 

At the momemt I just want to overwrite register EDI until the loop exits. When I figure out how to get that working I will mov the contents of EDI into the array. However, I'm getting a segmentation fault.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download