- I would like to deal with printing integers today.
- As I said before, we will hit some topics lightly
- The idea is to be able to accomplish the task at hand.
- We will be back to cover some of these topics in more detail later.
- My resources are both books.
- IN the end, we want this code.
%include "IO_DEFS.asm"
- This includes a file.
- IO_DEFS.asm
- It is a growing set of definitions for system calls.
- Jorgensen has a useful section (Appendix C) that we will look at later.
- I expect that I will continue to add to this file.
- You might want to copy this for each assignment.
- I will try to point out when I change it.
- You also might want my Makefile
- In the data section we declare a variable num
- Jorgensen discusses the data section in 4.4 (page 34)
-
name type value
-
dd
is for a 32 bit variable
-
num = 24601;
- We will be back!
- The bss section
- Jorgensen discuss this is on page 35.
-
name type count
-
resb
reserves 8 bit memory
-
char buffer[16];
- What the terminator say!
- The first part is to turn a number into a string
-
mov eax, [num]
- when [] show up in a move statement this involves memory
- In this case, num is the address that holds 24601
- So this moves 24601 from memory into eax.
-
lea edi, [buffer + 15]
-
lea
stands for load effective address
- This loads the address of buffer + 15 into edi
- Remember the place discussion above.
-
convert:
- This is a label.
- It is used in the jump below.
- I like labels on lines by themselves
- This makes it easier to add code later when you goof up.
-
dec edi
-
dec edi
mov edx, 0
mov ecx, 10
div ecx
- edx = 0 ; so the divide below works out.
- exc = 10
-
div
changes multiple registers
- eax = edx:eax / ecx
- edx = edx:eax % ecx
- carter page 44
- Terminator!
-
add dl, '0'
mov [edi], dl
- change the digit to a character
- Store it in the array
- Note, since we want a byte operation, we work on dl
-
test eax, eax
jnz convert
- Test instruction page 51 of Carter
- Does a logical and of the two registers
- But will not store the results.
- But sets a "flag" that says if they contain 0 or not.
- This is essentially (eax == 0)
- Combined with the
jnz
(jump not zero) this is the while loop.
- When we get done with the conversion
- edi holds the address of the last character written
- Jorgensen (page 328/239) tells us that to write a string
- e/rax needs to hold a 1 (SYS_WRITE)
- e/rdi needs to hold the file descriptor (STDOUT)
- e/rsi needs to hold the address of the string (starting)
- e/rdx needs to hold the number of characters to write.
- so we will move edi to esi,
- We need to compute the string length.
- buffer+16 is the end of the string (a high number)
- edi is the address of the last character
- So length = buffer+16 - edi
- Store this in edx
- Then set the two constants in rax and rdi.
- Note Jorgensen discusses exit on page 329.