Jump to content


Check out our Community Blogs

restin84's Content

There have been 16 items by restin84 (Search limited from 04-December 19)


Sort by                Order  

#643403 [SOLVED]Should I place local variables in registers before performing operati...

Posted by restin84 on 01 November 2012 - 08:04 PM in Assembly

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?



#643391 [SOLVED]Should I place local variables in registers before performing operati...

Posted by restin84 on 01 November 2012 - 07:39 PM in Assembly

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
  




#643379 [SOLVED]Should I place local variables in registers before performing operati...

Posted by restin84 on 01 November 2012 - 05:52 PM in Assembly

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




#643378 [SOLVED]Should I place local variables in registers before performing operati...

Posted by restin84 on 01 November 2012 - 05:32 PM in Assembly

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.



#643376 [SOLVED]Should I place local variables in registers before performing operati...

Posted by restin84 on 01 November 2012 - 05:23 PM in Assembly

If I am using a local variables to control a for loop, is it better to place these values into registers before doing comparisons between OR incrementing them?



#643173 [SOLVED]Why are library functions erasing the contents of registers?

Posted by restin84 on 30 October 2012 - 05:59 PM in Assembly

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.



#643164 [SOLVED]Why are library functions erasing the contents of registers?

Posted by restin84 on 30 October 2012 - 04:25 PM in Assembly

Okay thanks a lot. I need to study my caller save/callee save registers apparently. This is great practice considering I have a midterm on Thursday.



#643162 [SOLVED]Why are library functions erasing the contents of registers?

Posted by restin84 on 30 October 2012 - 04:04 PM in Assembly

So if I need to populate an array I shouldn't use %eax to hold the size of the array. Because if I'm using scanf inside the loop its just going to reset %eax to 0? Is scanf returning 0 because it executed successfully?



#643150 [SOLVED]Why are library functions erasing the contents of registers?

Posted by restin84 on 30 October 2012 - 02:33 PM in Assembly

I am using a for loop in an ia32 program to get user input to populate an array. It seems that every time I call printf or scanf the contents of the registers get set back to zero. Why is this happening?



#643095 [SOLVED]Question about getting user input(IA32)

Posted by restin84 on 29 October 2012 - 09:27 PM in Assembly

I will probably go with the first option. So how do I reference that area of memory to push it onto the stack before I call scanf?
Is it something like
pushl (%esp)
pushl $somestring
call scanf

Thanks for the help by the way.



#643089 [SOLVED]Question about getting user input(IA32)

Posted by restin84 on 29 October 2012 - 08:50 PM in Assembly

Hmmm. I'm not quite sure I know how to do that.



#643087 [SOLVED]Question about getting user input(IA32)

Posted by restin84 on 29 October 2012 - 08:27 PM in Assembly

So I have to create that in the bss segment for uninitialized gobal data? or say something like .comm n,4,4, in main put "movl n, %ecx" after I have set the value of n with scanf?

I did this and it works
    .comm A,100,4        #array A[]
    .comm m,4,4        #use to initialize local variable n
/************************************************************************/
/****************************READ-ONLY-DATA******************************/
    .section    .rodata
    .align 4
str1:    .string    "Enter size of array: "
frmt1:    .string    "%d"

    
teststr1:    .string "n=%d, i=%d"
teststr2:    .string    "n=%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
    movl    $0, %eax    #int i = 0
    movl    $0, %ecx    #int n = 0

    pushl    $str1
    call    printf
    addl    $4, %esp

    pushl    $m
    pushl    $frmt1
    call    scanf
    addl    $8, %esp
    movl    m, %ecx

    pushl    %ecx
    pushl    $teststr2
    call    printf
    addl    $8, %esp
    
    leave            #epilog
    ret            #epilog
 



#643081 [SOLVED]Question about getting user input(IA32)

Posted by restin84 on 29 October 2012 - 07:49 PM in Assembly

Hi guys. I'm having a little trouble getting user input in an assignment I have for my computer organization class. The program is in ia32. I have to read in a array size(local to main), and array elements(global array). We've been using 'call' in order to utilize the C language I/O functions printf and scanf.How do I use scanf to set the contents of a register to the input provided?What I've been trying is
.comm A,100,4 #array A[]/************************************************************************//****************************READ-ONLY-DATA******************************/.section .rodata.align 4str1: .string "Enter size of array: "frmt1: .string "%d"teststr1: .string "n=%d, i=%d"teststr2: .string "\nn=%d\n"/************************************************************************//****************************MAIN-FUNCTION*******************************/.text #text section.global main #declare main as globalmain:pushl %ebp #prolog save old %ebpmovl %esp, %ebp #prolog set new %ebpmovl $0, %eax #int i = 0movl $0, %ecx #int n = 0pushl $str1call printfaddl $4, %esppushl %ecx //<-----------I think my problem may be here? But I'm not sure of the fixpushl $frmt1call scanfaddl $8, %esppushl %ecxpushl $teststr2call printfaddl $8, %espleave #epilogret #epilog
I'm getting a segmentation faultI will also need to do the same thing to set the values for the array elements. What am I doing wrong?



#642404 [SOLVED] Jump table problems

Posted by restin84 on 19 October 2012 - 05:55 PM in Assembly

Hi guys. I'm just starting to do assembly language for my Computer Organization class. I have to translate a given program into ia32 assembly language. We are required to use a jump table to solve the problem

#Name: Doug Tait
#Assignment3
#10.13.2012
/****************PROGRAM TO TRANSLATE**********************/
//#include <stdio.h>

/*int month, numDays, year;
int valid = 1;

int main() {

printf("Enter month as an integer (1-12): ");
scanf("%d", &month);
printf("Enter year (e.g., 2012): ");
scanf("%d", &year);

switch (month) {
	 case 1: case 3: case 5:
	 case 7: case 8: case 10:
	 case 12:
		 numDays = 31;
		 break;
	 case 4: case 6:
	 case 9: case 11:
		 numDays = 30;
		 break;
	 case 2:
		 if ( ((year % 4 == 0) &&
		 !(year % 100 == 0))
		 || (year % 400 == 0))
		 numDays = 29;
		 else
		 numDays = 28;
		 break;
	 default:
	 valid = 0;
		 printf("Invalid month.\n");
}
if (valid)
	 printf("%d/%d has %d days\n", month, year, numDays);
}*/
/************************************************************************/
/*************************INITIALIZED-GLOBAL-DATA************************/
.data
.align 4
valid: .long 1 #int valid = 1
/************************************************************************/
/************************UNINITIALIZED-GLOBAL-DATA***********************/
.comm month,4,4 #int month
.comm numDays,4,4 #int numDays
.comm year,4,4 #int year
/************************************************************************/
/****************************READ-ONLY-DATA******************************/
.section .rodata
.align 4
str1: .string "Enter month as an integer(1-12): "
frmt1: .string "%d"
str2: .string "Enter year (e.g., 2012): "
frmt2: .string "%d"
frmt3: .string "\n%d, %d"
str3: .string "31 days"
str4: .string "30 days"
str5: .string "29 or 28"
str6: .string "invalid month"
str7: .string "Program done"
/************************************************************************/
/******************************JUMP-TABLE********************************/
.align 4
.JT:
.long .L0 #January
.long .L1 #February

/************************************************************************/
/****************************MAIN-FUNCTION*******************************/
.text
.globl main #declare main as global
main:
pushl %ebp
movl %esp, %ebp
pushl $str1 #push address of frmt1 onto stack
call printf #call printf library function
addl $4, %esp #pop arguments off stack
pushl $month #push address of month onto stack
pushl $frmt1 #push address of frmt2 onto stack
call scanf #call scanf library function
addl $8, %esp #pop arguments off stack
pushl $str2 #push address of frmt3 onto stack
call printf #call printf library function
addl $4, %esp #pop arguments off stack
pushl $year #push address of year onto stack
pushl $frmt2 #push address of frmt4 onto stack
call scanf #call scanf library function
addl $8, %esp #pop arguments off stack

movl $month, %eax
subl $1,%eax
cmpl $11, %eax
ja .L12
jmp *.JT(,%eax,4)

.L0:
pushl $str3
call printf
addl $4, %esp
jmp .L13

.L1:
pushl $str5
call printf
addl $4, %esp
jmp .L13

.L12:
pushl $str6
call printf
addl $4, %esp

.L13:
pushl $str7
call printf
addl $4, %esp

pushl year
pushl month
pushl $frmt3
call printf
addl $8, %esp
leave
ret

I've been running into problems getting this thing working. Right now I am just trying to handle the month = 1 and month = 2 cases. When I get that working I will build from there. The program currently takes a month and year input successfully but jumps straight to the case saying "invalid month". I'm following examples from my text and lecture slides but I seem to be missing something. Does anyone have some advice?



#642129 [SOLVED]Determine all (i,j) pairs such that A[i] = A[j]

Posted by restin84 on 16 October 2012 - 12:34 PM in General Programming

Okay. Thanks guys. I have to do an analysis also. I'm beginning to think the problem cannot be solved in any better than O(n^2) (worst case time that is)



#641992 [SOLVED]Determine all (i,j) pairs such that A[i] = A[j]

Posted by restin84 on 15 October 2012 - 11:34 AM in General Programming

Hi guys. I've been thinking over a homework problem for awhile now. Most of the solutions I have considered are inefficient so I was hoping someone may be able to give me a hint(not a solution) as to which direction I should move in.

Given an array of n>=2 numbers(possibly repeated), determine all pairs of indices (i,j) such that A[i] = A[j]

I know I can just use a nested for loop but the running time is O(n^2). I'm thinking there should be a more efficient way to do it.




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