ZUCC_ Computer system principle experiment_ Machine level representation of experiment 12 program -- branch and loop

Experimental report of City College of Zhejiang University

1, Purpose of the experiment:

Understand the common instructions of IA32 instruction system, the basic syntax of Linux assembly language, the writing and debugging skills of assembly language.

2, Experiment content:

  1. Read the experimental handout procedure 1, answer the questions raised in the experimental content, and fill in the following experimental steps. Screenshots are required to explain the experimental operation.
  2. Read the experimental handout procedure 2, answer the questions raised in the experimental content, and fill in the following experimental steps. Screenshots are required to explain the experimental operation.

3, Experimental steps:

1. Read the test1.c source code and the corresponding assembler given in the experimental handout, and complete the following questions:

A) According to the example style of Part b of if else in the first case of "assembly translation pseudo code corresponding to branch and loop structure" in the "basic knowledge" part of the experimental handout, write a goto version of the cond function in test1.c of this program in C language, perform the same calculation, and simulate the control flow of assembly code.

cond:

void cond(int a,int *p){
	if(p&&a>0)
		*p+=a;
}

goto version of cond function:

void cond(int a,int *p){
    int t = p && a > 0;
    if (!t)
        goto done;
    *p += a;
    goto done;
    
    done:
        return;
}

Assembly code:

cond:
	pushl	%ebp
	movl	%esp, %ebp
	movl	8(%ebp), %edx
	movl	12(%ebp), %eax
	testl	%eax, %eax
	je	.L4
	testl	%edx, %edx
	jle	.L4
	addl	%edx, (%eax)
.L4:
	popl	%ebp
	ret

It can be noted that the compilation results here are the same as the original compiled code, indicating that there is no significant change between goto version and the original version after O1 compilation

B) Add comments for each line of assembly code to explain the function of each statement.

According to the operation instructions given in the experimental handout, the screenshot shows the operation process from test1.c to assembly code:

Add comments for each line of assembly code to describe the operation performed by the program:

cond:
	pushl	%ebp
	movl	%esp, %ebp
	movl	8(%ebp), %edx   #Assign M(R(EBP)+8), i.e. a, to EDX
	movl	12(%ebp), %eax  #Assign M(R(EBP)+12), i.e. p address to EAX
	testl	%eax, %eax      #Operate eax & eax and modify the identifier
	je	.L4                 #If EAX is 0, jump to. L4
	testl	%edx, %edx      #Operate EDX & EDX and modify the identifier
	jle	.L4                 #If EDX is 0, jump to. L4
	addl	%edx, (%eax)    #M(R(EAX)) is assigned M(R(EAX))+EDX
.L4:
	popl	%ebp
	ret

The screenshot shows the execution results of each line of statements that need to be annotated in the assembly code segment of the above cond function during GDB debugging:

Set breakpoint

Initialization status

a assigned to EDX

p address assigned to EAX

EAX & EAX

EDX & EDX

M(R(EAX)) is assigned

C) Please explain why there is only one if statement in C language code, while assembly code contains two conditional branches.

The assembly code splits the original if judgment statement "P & &a > 0" into two parts, and combines two branches to represent an IF statement

2. Read the test2.c source code and the corresponding assembler given in the experimental handout, and complete the following questions

test2.c

int dw_loop(int x,int y,int n)
{
	do{
		x += n;
		y *= n;
		n--;
	}
	while((n>0)&&(y<n));
	return x;
}
int main()
{
		dw_loop(5,4,3);
}

test2.s

	.file	"test2.c"
	.text
.globl dw_loop
	.type	dw_loop, @function
dw_loop:
	pushl	%ebp
	movl	%esp, %ebp
	movl	8(%ebp), %eax
	movl	12(%ebp), %ecx
	movl	16(%ebp), %edx
.L3:
	addl	%edx, %eax
	imull	%edx, %ecx
	subl	$1, %edx
	testl	%edx, %edx
	jle	.L5
	cmpl	%edx, %ecx
	jl	.L3
.L5:
	popl	%ebp
	ret
	.size	dw_loop, .-dw_loop
.globl main
	.type	main, @function
main:
	leal	4(%esp), %ecx
	andl	$-16, %esp
	pushl	-4(%ecx)
	pushl	%ebp
	movl	%esp, %ebp
	pushl	%ecx
	subl	$12, %esp
	movl	$3, 8(%esp)
	movl	$4, 4(%esp)
	movl	$5, (%esp)
	call	dw_loop
	addl	$12, %esp
	popl	%ecx
	popl	%ebp
	leal	-4(%ecx), %esp
	ret
	.size	main, .-main
	.ident	"GCC: (GNU) 4.1.2 20080704 (Red Hat 4.1.2-48)"
	.section	.note.GNU-stack,"",@progbits

A) Read the assembler and fill in the register usage table below.

registerCorresponding variableInitial value
EAXx5
ECXy4
EDXn3

B) Point out the test expr (test condition expression) and body statement (loop body) in C code, as well as the corresponding lines in assembly code.

Test expr (test condition expression) and corresponding assembly statement:

(n>0)&&(y<n)

Corresponding assembly statement

Testl% EDX,% EDX and cmpl% EDX,% ECX

Body statement (loop body) and corresponding assembly statement:

x += n;
y *= n;
n--;

Corresponding assembly statement

addl	%edx, %eax
imull	%edx, %ecx
subl	$1, %edx

C) Add comments for each instruction of the assembly code to describe the operation of the program.

dw_loop:
	pushl	%ebp
	movl	%esp, %ebp
	movl	8(%ebp), %eax         #Assign M(R(EBP)+8), i.e. x, to EAX
	movl	12(%ebp), %ecx        #Assign M(R(EBP)+12), i.e. y, to ECX
	movl	16(%ebp), %edx        #Assign M(R(EBP)+16), i.e. n, to EDX
.L3:
	addl	%edx, %eax            #Assign R(EDX)+R(EAX), that is, n+x to EAX
	imull	%edx, %ecx            #Assign R(EDX)*R(ECX), i.e. n*y to ECX
	subl	$1, %edx              #Reduce EDX by 1, i.e. n--
	testl	%edx, %edx            #Perform EDX & EDX and modify the identifier
	jle	.L5                       #If EDX is n < = 0, jump to. L5
	cmpl	%edx, %ecx            #Perform ECX - EDX and modify the identifier
	jl	.L3                       #If ecx-edx < 0, i.e. y < n, jump to. L3
.L5:
	popl	%ebp
	ret

The screenshot shows the above DW during GDB debugging_ The assembly code segment of loop function, and the execution result of each line of statement that needs to be annotated:

Set breakpoint

Initial state

x. y, n assignment

n+x assigned to EAX

n*y assigned to ECX

n–

EDX & EDX

Satisfy continue

ECX - EDX

Dissatisfied exit

Tags: Back-end

Posted on Wed, 01 Dec 2021 01:26:51 -0500 by herando