《Programming from the Ground Up》學習第3天,p49-p74總結,總計26頁。
一、技術總結
1.function
(1)定義
p49, Functions are unit of code that do a defined piece of work on specified types of data。
函式是在指定型別的資料上完成所定義的某個工作的程式碼單元。
(2)parameter
p49, The data items a function is given to process are called it's parameters。
給予函式處理的資料項稱為函式的引數。
(3)interface
p49, The parameter list and the processing expectations of a function(what it is expected to to with the parameters) are called the function's interface。
函式引數列表和預期處理結果稱為函式的介面。
2.primitive functions/primitives
p50,However, ultimately there are things that you cannot write functions for which must be provided by the system. Those are called primiteive function(or just primiteives)-they are teh basics which everything else is built off of.
這些由系統提供的函式稱為原函式(或原語)。
3.stack
(1)定義
p51,Each computer program that runs uses a region of memory called the stack to enable functions to work properly.
The computer's stack lives at the very top address of memory.
stack(棧)就是memory(記憶體)的一塊區域,位於記憶體的最頂端。這是物理意義的上的。
4.power.s
#PURPOSE: Program to illustrate how functions work
# This program will compute the value of
# 2^3 + 5^2
#
#Everything in the main program is stored in registers,
#so the data section doesn't have anything
.section .data #.data的作用是什麼?
.section .text #.text的作用是什麼?
.global _start
_start:
pushq $3 #push second argument
pushq $2 #push first argument
call power # call the function
add $8, %rsp #movqe the stack pointer back
pushq %rax #save the first answer before
#calling the next function
pushq $2 #push second argument
pushq $5
call power #call the function
add $8, %rsp #movqe the stack pointer back
popq %rbx #The second answer is already
#in %rax. We save the
#first answer onto the stack,
#so now we can just pop it
#out into %rbx
add %rax, %rbx #add them together
#the result is in %rbx
movq $1, %rax #exit (%rbx is returned)
int $0x80
#PURPOSE: This function is used to compute
#the value of a number raised to a power.
#
#INPUT: First argument-the base number
#Second argument-the power to raise it to
#
#OUTPUT: Will give the result as a return value
#
#NOTES: The power must be 1 or greater
#
#VARIABLES:
#%rbx - holds the base number
#%rcx - holds the power
#
#-4(%rbp) - holds the current result
#
# %rax is used for temporary storage
#
.type power, @function
power:
pushq %rbp #save old base pointer
movq %rsp, %rbp # make stack pointer the base pointer
sub $4, %rsp #get room for our local storage
movq 8(%rbp), %rbx #put first argument in %rax
movq 12(%rbp), %rcx #put second argument in %rcx
movq %rbx, -4(%rbp) #store current result
power_loop_start:
cmp $1, %rcx #if the power is 1, we are done
je end_power
movq -4(%rbp), %rax #movqe the current result into %rax
imul %rbx, %rax #multiply the current result by
#the base number
movq %rax, -4(%rbp) #store the current result
dec %rcx #decrease the power
jmp power_loop_start # run for the next power
end_power:
movq -4(%rbp), %rax # return value goes in %rax
movq %rbp, %rsp #restore the stack pointer
popq %rbp #restore the base pointer
ret
書裡使用的是32位的電腦,但我們現在的電腦大多是64位的,如果執行書中的程式碼就會提示:Error: invalid instruction suffix for `push',這裡對程式碼做了修改,改為64位電腦裡的暫存器。
二、英語總結
無。
三、其它
1.8、16、32、64位電腦裡暫存器的名字
因為不同教材使用的暫存器名稱不一樣,容易對初學者造成困擾,這裡把8位、16位、32位、64位電腦裡的暫存器名稱全部列出來,這樣在看到這些名字的時候我們就知道作者使用的是多少位的電腦。同時我們儘量在對應位數的電腦裡去執行這些程式碼。
r8 = AL AH BL BH CL CH DL DH
r16 = AX BX CX DX BP SP SI DI
r32 = EAX EBX ECX EDX EBP ESP ESI EDI
r64 = RAX RBX RCX RDX RBP RSP RSI RDI R8 R9 R10 R11 R12 R13 R14 R15
2.32和64位電腦裡暫存器呼叫區別
(1)x86 Assembly pushl/popl don't work with "Error: suffix or operands invalid"
https://stackoverflow.com/questions/5485468/x86-assembly-pushl-popl-dont-work-with-error-suffix-or-operands-invalid
(2)What are the calling conventions for UNIX & Linux system calls (and user-space functions) on i386 and x86-64
https://stackoverflow.com/questions/2535989/what-are-the-calling-conventions-for-unix-linux-system-calls-and-user-space-f
四、參考資料
1. 程式設計
(1)Jonathan Bartlett,《Programming From The Ground Up》:https://book.douban.com/subject/1787855/
2. 英語
(1)Etymology Dictionary:https://www.etymonline.com
(2) Cambridge Dictionary:https://dictionary.cambridge.org
歡迎搜尋及關注:程式設計人(a_codists)