Assembly language and addressing modes
แนะนำใหม่
21:25
สถาปัตยกรรม Von Neumann
ตลอดประวัติศาสตร์ส่วนใหญ่ เครื่องจักรทำงานเพียงอย่างหนึ่ง กระดุมทอผ้า คำนวณบวก เพื่อทำให้มันทำอะไรใหม่ คุณต้องสร้างมันใหม่ จากนั้นก็มาถึงสิ่งที่ดูเรียบง่ายแต่ลวงตา…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| instruction/ɪnˈstrʌkʃn/ | A command that tells the processor what action to perform. |
| initial orders | The first instructions used to start EDSAC and translate programs from paper tape. |
| paper tape | A long strip of paper used to store information for a computer to read. |
| assembler/əˈsemblə/ | อสมบิลเลอร์ |
| mnemonic/nɪˈmɒnɪk/ | มเนโมนิค |
| bit patterns | Arrangements of binary digits that represent instructions or data in a computer. |
| instruction set/ɪnˈstrʌkʃn set/ | ชุดคำสั่ง |
| machine code/məˈʃiːn kəʊd/ | โค้ดเครื่อง |
| Assembly language/əˈsemblɪ ˈlæŋɡwɪdʒ/ | The readable form. |
| processor/ˈprəʊsesə/ | The component that executes instructions and performs the operations of a computer or similar system. |
| architecture/ˈɑːkɪtektʃə/ | The design of a computer system, including its processor and instruction set. |
| operand/ˈɒpərænd/ | ตัวดำเนินการ |
| high-level language/haɪ ˈlevl ˈlæŋɡwɪdʒ/ | A programming language that expresses instructions using abstractions rather than machine-specific operations. |
| statement/ˈsteɪtmənt/ | A line of code that tells a program what to do. |
| opcode/ˈɒpkəʊd/ | โอเปค |
| symbolic address | A name used in a program to represent a memory address. |
| Pass 1 | The first reading of assembly source code to record labels and their addresses. |
| source/sɔːs/ | The original program written by a programmer before translation into machine code. |
| symbol table/ˈsɪmbl ˈteɪbl/ | ตารางสัญลักษณ์ |
| label/ˈleɪbl/ | ฉลาก |
| address/əˈdres/ | An identifier specifying a location in memory or on a network. |
| Pass 2 | The second reading of assembly source code to produce machine code. |
| forward references/ˈfɔːwəd ˈrefrənsɪz/ | การอ้างอิงไปข้างหน้า |
| general-purpose register | A small storage place inside a processor used to hold data during operations. |
| accumulator/əˈkjuːmjʊleɪtə/ | A register used to hold intermediate arithmetic or logical results during processing. |
| index register/ˈɪndeks ˈredʒɪstə/ | A register that holds a value added to an address during indexed addressing. |
| denary/ˈdiːnəri/ | The base-ten number system, using the digits zero through nine. |
| binary/ˈbaɪnəri/ | A number system using base two, with the digits zero and one. |
| hexadecimal/ˌheksəˈdesɪml/ | A base-sixteen number system using digits zero to nine and letters A to F. |
| Data movement | Operations that move data between registers and memory or load values into registers. |
| Input and output | Operations that receive data from outside a computer or send data out. |
| Arithmetic/əˈrɪθmətɪk/ | Calculations with numbers, such as adding and subtracting. |
| Compare/kəmˈpeə/ | An operation that checks two values and records the result in a flag. |
| Jumps | Instructions that change which instruction the processor executes next. |
| Bit manipulation | Operations that change or test individual bits within a value. |
| memory location | A place in memory that holds data and has an address. |
| loop/luːp/ | A structure that repeats a sequence of instructions. |
| addressing mode/əˈdresɪŋ məʊd/ | โหมดการเข้าถึง |
| immediate addressing/ɪˈmiːdɪət əˈdresɪŋ/ | การเข้าถึงแบบทันที |
| direct addressing/daɪˈrekt əˈdresɪŋ/ | การเข้าถึงโดยตรง |
| indirect addressing/ɪndaɪˈrekt əˈdresɪŋ/ | การเข้าถึงแบบอ้อม |
| indexed addressing/ˈɪndekst əˈdresɪŋ/ | การเข้าถึงแบบดรรชนี |
| effective address | The final memory address calculated by the processor to access an operand. |
| relative addressing/ˈrelətɪv əˈdresɪŋ/ | การเข้าถึงแบบสัมพัทธ์ |
| offset/ˈɒfset/ | A value added to an address to find another memory location. |
| operand field | The part of an instruction that specifies an operand or helps locate it. |
| array/əˈreɪ/ | An indexed collection of elements arranged in one or more dimensions. |
| element/ˈelɪmənt/ | A substance made of atoms with the same proton number. |
| flag/flæɡ/ | A bit that records whether a particular condition or event has occurred. |
| conditional jump | An instruction that changes the next instruction only when a condition is met. |
| mask/mæsk/ | A pattern of bits used with logical operations to select, test, set or clear particular bits. |
คำสั่งสามสิบหนึ่งบรรทัดเรียงกันบนสวิตช์
- ในปี ค.ศ. 1949 คอมพิวเตอร์ EDSAC ในเคมบริดจ์เริ่มทำงานจากคำสั่ง 31 บรรทัดที่ตั้งอยู่บนสวิตช์ ซึ่งเขียนโดย David Wheeler และเรียกว่า "initial orders"
- งานทั้งหมดของพวกเขาคือการอ่านตัวอักษรเช่น A และ S จากเทปกระดาษและแปลงให้เป็นตัวเลขที่เครื่องเข้าใจได้จริง: นี่คือ assembler ตัวแรก
- ตั้งแต่นั้นมา โปรแกรมเมอร์ก็เขียน mnemonics แทนรูปแบบบิต และชุดคำสั่งของข้อสอบก็เป็นลูกหลานโดยตรงของสิ่งนี้
- บทเรียนนี้เกี่ยวกับการแปลนั้น การติดตามโปรแกรมด้วยมือ และห้าวิธีที่คำสั่งสามารถระบุตำแหน่งของข้อมูลได้
รหัสเครื่องและภาษาแอสsemblี
- โปรเซสเซอร์รัน machine code: รูปแบบบิตเฉพาะสำหรับสถาปัตยกรรมหนึ่งๆ แต่ละอันคือ operation code และ operand
- Assembly language คือรูปแบบที่อ่านง่าย Each instruction เขียนด้วย mnemonic เช่น
LDD,ADDหรือJMPและแต่ละ assembly instruction จะกลายเป็น machine-code instruction ที่แน่นอนหนึ่งinstruction - Assembler แปลมัน ความสัมพันธ์คือหนึ่งต่อหนึ่ง ซึ่งเป็นสิ่งที่ทำให้แอสsemblีแตกต่างจาก high-level language ที่ statement หนึ่งกลายเป็นหลายคำสั่ง

Mnemonic แต่ละอันกลายเป็น opcode หนึ่ง; symbolic address แต่ละอันกลายเป็นตัวเลข

Assembly language is:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Assembly uses mnemonics and maps one-to-one to machine code; an assembler translates it.
Assembler แบบสองรอบ
- Pass 1 อ่าน source และสร้าง symbol table: ทุกครั้งที่ label เช่น
LOOP:ปรากฏขึ้น ADDRESS จะถูกบันทึก ไม่มีโค้ดที่ถูกผลิตออกมา - Pass 2 อ่าน source อีกครั้งและสร้างโค้ด โดยแทนที่ mnemonics ด้วย opcode และแทนที่ symbolic addresses ด้วยตัวเลขจาก symbol table
- ต้องใช้สองรอบเนื่องจาก forward references:
JMP LOOPอาจปรากฏก่อนบรรทัดLOOP:ถูกมองเห็น ดังนั้น ADDRESS ของมันจึงไม่ทราบในรอบแรก

What does pass 1 of a two-pass assembler do?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Pass 1 records where each label is (the symbol table); pass 2 then generates code, using the table to resolve label references.
Put the two-pass assembler's work in order.
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Pass 1 finds all the labels first, so pass 2 can resolve even a jump to a label defined later.
Why does the assembler need two passes?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A jump may target a label that appears later in the source; pass 1 finds all labels first so pass 2 can resolve them.
ตัวอย่างการคำนวณ: การประยุกต์ใช้สองรอบ
- โปรแกรมเริ่มต้นที่ ADDRESS 100
100 LDD COUNT
101 LOOP: DEC ACC
102 CMP #0
103 JPN LOOP
104 END
105 COUNT: 5
- Pass 1 นับ ADDRESS ของแต่ละบรรทัดและบันทึก labels:
LOOP= 101,COUNT= 105 ไม่มีการเขียนอะไรอื่น - Pass 2 แปลบรรทัดต่อบรรทัด
LDD COUNTกลายเป็น opcode สำหรับLDDพร้อม operand 105;JPN LOOPกลายเป็น opcode สำหรับJPNพร้อม operand 101 หากไม่มี pass 1 บรรทัดแรกจะไม่สามารถแปลได้
How a two-pass assembler works
Step through it. The assembler reads your code twice: pass 1 just finds where every label lives, so pass 2 can fill in the addresses — that is how a jump to a label defined ช้าลง still works.
In the worked example, pass 1 records the label LOOP with the address ____ in the symbol table.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The program starts at 100, so the second line, LOOP: DEC ACC, occupies 101. COUNT, on the sixth line, is 105.
ชุดคำสั่ง
- ชุดคำสั่งของเคมบริดจ์มี general-purpose register หนึ่งตัวคือ accumulator ACC และ index register IX Operand
#nเป็นฐานสิบ,Bnเป็นฐานสอง,&nเป็นฐานหกteen, และ<address>เป็นหมายเลขสถานที่หรือ label - การเคลื่อนย้ายข้อมูล:
LDM #n,LDD <address>,LDI <address>,LDX <address>,LDR #n,MOV IX,STO <address>อินพุตและเอาต์พุต:IN,OUT - การคำนวณทางคณิตศาสตร์:
ADD,SUB,INC,DECเปรียบเทียบ:CMP,CMIการกระโดด:JMPแบบไม่เงื่อนไข,JPEและJPNแบบเงื่อนไข การจัดการบิต:AND,OR,XOR,LSL,LSRจากนั้นEND - "คำสั่งถูกจัดกลุ่ม": ชื่อกลุ่มและยกตัวอย่างคำสั่งหนึ่งจากแต่ละกลุ่ม
Which of these are groups in the instruction set? Select ทั้งหมด that apply.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Data movement, input/output, arithmetic, compare, conditional and unconditional jumps, and bit manipulation. A spreadsheet is application software, far above this level.
ตัวอย่างการคำนวณ: การติดตามโปรแกรม
- วาดตารางที่มีคอลัมน์สำหรับ ACC และสำหรับทุก memory location ที่โปรแกรมใช้ แล้วอัปเดตทีละบรรทัดจนกว่าจะถึง
END
100 LDM #0
101 STO TOTAL
102 LDD TOTAL
103 ADD #5
104 STO TOTAL
105 LDD COUNT
106 DEC ACC
107 STO COUNT
108 CMP #0
109 JPN 102
110 END
111 TOTAL: 0
112 COUNT: 3
- Pass 1 ของลูป: ACC 0 → 5 (TOTAL = 5), COUNT 3 → 2, เปรียบเทียบไม่เท่ากัน, กระโดด Pass 2: TOTAL = 10, COUNT = 1, กระโดด Pass 3: TOTAL = 15, COUNT = 0, เปรียบเทียบเท่ากัน, ไม่กระโดด,
END - ค่าสุดท้าย: TOTAL = 15, COUNT = 0, ACC = 0 เขียนแถวใหม่ก็ต่อเมื่อค่าเปลี่ยนไปและไม่ควรข้ามขั้นตอนการเปรียบเทียบเด็ดขาด
In the traced program, what is the value of TOTAL when END is reached?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The loop adds 5 to TOTAL once for each of the three passes: 5, 10, 15.
โหมดการอ้างอิงที่อยู่ (Addressing modes)
- Addressing mode บอกว่าโปรเซสเซอร์หา operand ได้อย่างไร
| Mode | ตำแหน่งของ operand | ตัวอย่าง |
|---|---|---|
| immediate addressing | ค่าอยู่ในตัวคำสั่งเอง | LDM #10 โหลด 10 |
| direct addressing | คำสั่งมี ADDRESS; ใช้ค่าที่เก็บไว้นั้น | LDD 200 โหลดเนื้อหาของ 200 |
| indirect addressing | Address มีอีก address หนึ่งซึ่งมีข้อมูลอยู่ | LDI 200 |
| indexed addressing | effective address = address ที่ให้มา + index register | LDX 200 กับ IX |
| relative addressing | Address เป็น offset จากคำสั่งปัจจุบัน | การกระโดด |

*ฟิลด์ operand เดียวกัน ความหมายสี่แบบต่างกัน

In immediate addressing (e.g. LDM #10), the operand is:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Immediate addressing uses the literal value in the instruction (here, 10).
Match each addressing mode to its meaning.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Immediate = value; direct = address of the value; indirect = address of an address; indexed = base + index.
ตัวอย่างการคำนวณ: หนึ่ง operand สี่คำตอบ
- Memory: Location 200 เก็บ 300, Location 300 เก็บ 7, Location 202 เก็บ 9 Index register IX เก็บ 2
LDM #200ใส่ 200 ลงใน ACC: Immediate, ตัวเลข itselfLDD 200ใส่ 300 ลงใน ACC: Direct, เนื้อหาของ 200LDI 200ใส่ 7 ลงใน ACC: Indirect, เนื้อหาของ address ที่พบที่ 200LDX 200บวก IX เข้ากับ 200 และโหลดเนื้อหาของ 202: 9 Operand เดียวกันเขียนสี่แบบ สี่ค่าต่างกัน

เหตุผลที่มี indexed addressing
- Array คือชุดของตำแหน่งที่ต่อเนื่องกัน
LDX 200พร้อม IX = 0, 1, 2, … จะอ่าน element 0, 1, 2, … โดยไม่เปลี่ยนคำสั่ง - รูปแบบ loop:
LDR #0เพื่อตั้ง index เป็นศูนย์,LDX ARRAYเพื่อดึงข้อมูล, ทำสิ่งใดสิ่งหนึ่ง,INC IXเปรียบเทียบกับความยาว,JPNกลับไปใหม่ - การเข้าถึงโดยตรงจำเป็นต้องใช้คำสั่งแยกสำหรับแต่ละข้อมูล; การเข้าถึงแบบดรรชนีใช้เพียงหนึ่งเดียว.

Indexed addressing is most useful for:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The effective address is base address + index register, so increasing the index walks through an array.
เปรียบเทียบและกระโดด
CMP #nหรือCMP <address>เปรียบเทียบ ACC กับค่าหนึ่งและตั้งค่าบิตสถานะ (flag) มัน ไม่ เปลี่ยนแปลงค่าของ ACC.JPE <address>จะกระโดดหากผลการเปรียบเทียบล่าสุดพบว่าค่าเท่ากัน;JPN <address>จะกระโดดหากค่าไม่เท่ากัน;JMP <address>กระโดดเสมอ.- คำสั่งในภาษาระดับสูง
IFจะกลายเป็นCMPตามด้วยการกระโดดตามเงื่อนไข; ลูปจะประกอบด้วยขั้นตอนเปรียบเทียบและการกระโดดกลับไปยังฉลาก.

CMP #0 changes the value held in the accumulator.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A compare only sets the flag that the next conditional jump reads. The ACC is unchanged, which is why the trace shows no new ACC value on a CMP line.
คะแนนที่หลุดหายไป
LDM #10โหลดตัวเลข 10;LDD 10โหลดเนื้อหาจากตำแหน่งที่ 10. ส่วน#คือความแตกต่างทั้งหมด.#เป็น denary,Bเป็น binary,&เป็น hexadecimal.AND &0FและAND B00001111เป็น mask เดียวกัน;AND #15ก็ similarly.CMPไม่ touched ค่า ACC; มันตั้งค่าบิตสถานะเท่านั้น.JPNจะกระโดดเมื่อค่า ไม่ เท่ากัน.- การผ่านครั้งที่ 1 ของโปรแกรมแปลภาษาแอสเซมบลี ไม่ได้ สร้างโค้ด. ผลลัพธ์คือตารางสัญลักษณ์ และการอ้างอิงถึงอนาคตคือเหตุผลในการมีอยู่ของมัน.
คุณเข้าใจแล้ว
- โค้ดเครื่อง คือรูปแบบบิต; แอสเซมบลี คือคำนำหน้า (mnemonics), แบบหนึ่งต่อหนึ่ง, ถูกแปลโดย โปรแกรมนำเข้า
- สองรอบ: รอบที่ 1 สร้าง ตารางสัญลักษณ์ ของที่อยู่ฉลาก, รอบที่ 2 สร้างโค้ด; จำเป็นสำหรับการ อ้างอิงถึงอนาคต
- เทรส ด้วยตารางของ ACC และทุกตำแหน่ง,หนึ่งแถวต่อการเปลี่ยนแปลง; CMP ตั้งค่าบิตสถานะ, JPE/JPN อ่านค่ามัน
- การเข้าถึง: ทันที
#nค่า · โดยตรง เนื้อหาของที่อยู่ · อ้อม ที่อยู่ของที่อยู่ · ดรรชนี ที่อยู่ + IX (อาเรย์) · สัมพัทธ์ การเบี่ยงเบน
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้