Programming From The Ground Up Chapter 3

Chapter 3:

Know The Concepts

1) What does if mean if a line in the program starts with the ’#’ character?

The ‘#’ character declares the line or the remainder of the line as a comment. This line will not change the program in any way. Comments are extremely helpful for programmers as it allows them to take notes in the code which not only helps them but also others who may look at the code.

2) What is the difference between an assembly language file and an object code file?

An assembly language file is a more human-readable form of the instructions a computer understands. When an assembly language file is assembled (using a command such as the example: “as exit.s -o exit.o” we are transforming an assembly language file or source code into an object code file. This is actually just converting our mostly human-readable form of instructions into instructions that are readable by the machine. However an object file is still not a complete program without the use of a linker first.

3) What does the linker do?

Object files are not capable of being ran without utilizing a linker. Typically there will be multiple source files that are converted into multiple object files and then the object files will be linked together using a linker. The linker also adds additional information that allows the program to run. To obtain the final program you can use the following command which will link the file “ld exit.o – exit”

4) How do you check the result status code of the last program you ran?

“Echo $?” will return the status code from the last run process. 0 means no error and other values may have different meanings. In the example in chapter 3 we altered this by setting a value to the %ebx register. For the maximum program the “echo $?” command will actually return us the maximum value as long as it is within the normal status code range.

5) What is the difference between movl $1, %eax and movl 1, %eax?

“Movl $1, %eax” will copy the value 1 into the register eax while “movl 1, %eax” will copy the value located at memory location 1 into the eax register. The ‘$’ character in front of a number indicates immediate mode. Without the ‘$’ character it would be direct addressing mode.

6) Which register holds the system call number?

The eax register stores the system call.

7) What are indexes used for?

Indexes are used for accessing a specific byte or range of bytes. An example of this is with a string of bytes, in order to access the 3rd byte we can use an index of 2 since we start with 0.

8)  Why do indexes usually start at 0?

The index is used as an offset. Therefore the first element is contained in the current memory location, an offset of 0. The next element will be exactly one element away which is why it will have an index of 1.

9) If I issued the command movl data_items(,%edi,4), %eax and data_items was address 3634 and %edi held the value 13, what address would you be using to move into %eax?

Indexed addressing mode:

Starts at data_items, and adds 4 * %edi to that address, and loads the value into %eax.

3634 + (4 * 13) = 3686. So the final address that is moved into %eax is 3686.

10) List the general-purpose registers.

General-purpose registers (8):

AX – accumulator register, Used in arithmetic operations

CX – Counter register, Used in shift/rotate instructions and loops

DX – Data register, Used in arithmetic operations and I/O operations

BX – Base register, Used as a pointer to data

SP – Stack Pointer register, Pointer to the top of the stack

BP – Stack Base Pointer register, Points to the base of the stack

SI – Source Index register, Pointer to a source in stream operations

DI – Destination Index register, Pointer to a destination in stream operations

11) What is the difference between movl and movb?

Movl will move data a word at a time or 4 bytes while movb will only move data a byte at a time. Depending on the size of the register your use of movl and movb may differ. For example in this chapter we work with word sized registers. Therefore, if we wanted to work with two bytes at a time with the register %eax we would have to use %ax (least significant half of %eax). If we wanted to work with one byte we would have to use one of the two bytes of %ax which are %al and %ah (%al is least significant and %ah is the most significant).

12) What is flow control?

Flow control is the order in which individual statements, instructions, or function calls are executed or evaluated. In chapter 3 we break down the loop we would create in order to find the maximum value in the list of integers. Control flow in this case is the steps and their order.

13) What does a conditional jump do?

A conditional jump is quite simply an if statement. With a conditional jump the resultant path varies based on the results of a previous comparison or calculation.

14) What things do you have to plan for when writing a program?

When planning a future program the main thing to keep in mind that a computer will be reading the code, not a human. When computers read code they read everything as literal as possible. They will do whatever the code tells us whether or not it breaks something or works perfectly. It is important to understand this and plan ahead when creating code. It is best to plan out the variables you will be using, data types, conditional statements, loops, and other related tasks before writing the program.

15) Go through every instruction and list what addressing mode is being used for each operand.

Not sure if this means every instruction possible or just for the example in the chapter but I will do the instructions from the example.

Movl $0, %edi direct addressing mode

Movl data_items(,%edi,4), indexed addressing mode

Movl %eax, %ebx indirect addressing mode

Cmpl $0, %eax direct addressing mode

Je loop_exit indirect addressing mode

Incl %edi

Movl data_items(,%edi,4), indexed addressing mode

Cmpl %ebx, %eax indirect addressing mode

Jle start_loop indirect addressing mode

Movl %eax, %ebx indirect addressing mode

Jmp start_loop indirect addressing mode

Movl $1, %eax direct addressing mode

Use the Concepts

1) Modify the first program to return the value 3.

.section .data

.section .text

.global _start

_start:

movl $1, %eax

movl $3, %ebx

int $0x80

The only change made in this program was the value 3 being placed in the line “movl $3 %ebx” This is because ebx holds the return status for the system call.

2) Modify the maximum program to find the minimum instead.

.section .data

data_items:

.long 3,67,34,222,45,75,54,34,44,33,22,11,66,0

.section .text

.global _start

_start:

movl $0, %edi

movl data_items(,%edi,4), %eax

movl %eax, %ebx

start_loop:

incl %edi

movl data_items(,%edi,4), %eax

cmpl $0, %eax

je loop_exit

cmpl %ebx, %eax

jge start_loop

movl %eax, %ebx

jmp start_loop

loop_exit:

movl $1, %eax

int $0x80

The changes made in this program include moving the instruction that checks if a 0 is found as well as changing the jle instruction to jge. We had to move the instruction that checks for a 0 because in the original position it was checking if the 0 was less than the current least value. By moving it after the movl data_items(,%edi,4), %eax which changes %eax to the next value in data_items we are able to check if this value is equal to 0 before it goes back to the start of the loop. Lastly, jge is for greater or equal to and jle is for less than or equal to.

3) Modify the maximum program to use the number 255 to end the list rather than the number 0

The only change that would be made here is the “cmpl $0, %eax” line. The ‘0’ here would be changed to ‘255’

4) Modify the maximum program to use an ending address rather than the number 0 to know when to stop.

.section .data

data_items:

.long 3,67,34,222,45,75,54,34,44,33,22,11,66,0

.section .text

.globl _start

_start:

movl $0, %edi

movl data_items(,%edi,4), %eax

movl %eax, %ebx

movl $13, %ecx #14 objects so we will need an index of 13 to obtain ending address

start_loop:

cmpl data_items(,%ecx,4) #using %ecx=13 and a multiplier of 4 to find ending address

je loop_exit

….

As you can see the two changes we made were an additional register with the value 13 and a change in the cmpl operator. The change we made checks if the ending address matches the address of %eax and if so it will end the program. We find the ending address by using indexed addressing mode ‘data_items(,%edi,4)’

5) Modify the maximum program to use a length count rather than the number 0 to know when to stop.

The only change made was commenting out the initial check:

cmpl $0, %eax

And replace this with

cmpl $13, %edi

We use 13 because there are 14 digits in the list.

6) What would the instruction movl _start, %eax do? Be specific, based on your knowledge of both addressing modes and the meaning of _start. How would this differ from the instruction movl $_start, %eax?

movl _start, %eax

This being indirect addressing mode will read 4 bytes from memory at the address of _start and copy it into %eax.

movl $_start, %eax

This is direct addressing mode and will load the address of _start into %eax.

Going Further

1) Modify the first program to leave off the int instruction line. Assemble, link, and execute the new program. What error message do you get. Why do you think this might be?

“Segmentation fault” is the error message provided.

Segmentation fault – When a program attempts to access memory it has not been assigned by the operating system or is not allowed to access

The reason we get this error is because without the “int $0x80” line the program does not know when to end. This is an issue because the program will begin executing whatever random bytes are in memory following this program. A segmentation fault occurs because the random data is very likely to be memory access outside of the process memory which would mean it does not have access.

2) So far, we have discussed three approaches to finding the end of the list – using a special number, using the ending address, and using the length count. Which approach do you think is best? Why? Which approach would you use if you knew that the list was sorted? Why?

I would think that the special number would be the best approach as it is as simple as storing a value in a register and doing a cmpl operation. This could be an issue of course if that number is being used but you could also use a character. If the list was sorted I would likely use the length count method as it is basically the same as the ending address just in a simpler fashion.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: