--- title: ASM final --- # Assembly Language<br> final NTNU 組合語言 ##### [Back to Note Overview](https://reurl.cc/XXeYaE) ##### [Back to Assembly Language](https://hackmd.io/@NTNUCSIE112/HkSk0OxBD) {%hackmd @sophie8909/pink_theme %} ###### tags: `NTNU` `CSIE` `選修` `Assembly Language` ## Ch.07 Integer Arithmetic ### 7.1 Shift and Rotate Instructions ***Bit shifting*** means to move bits right and left inside an operand. x86 processors provide following instructions, all affecting the **Overflow** and **Carry** flags. | instruction | | | ----------- | ---------------------------- | | SHL | Shift left | | SHR | Shift right | | SAL | Shift arithmetic left | | SAR | Shift arithmetic right | | ROL | Rotate left | | ROR | Rotate right | | RCL | Rotate carry left | | RCR | Rotate carry right | | SHLD | Double-presicion shift left | | SHRD | Double-presicion shift right | #### 7.1.1 Logical Shift and Arithmetic Shifts - **Logical Shift** - fills the newly created bit position with $0$. ![](https://i.imgur.com/K9N1INm.png) - **Arithmetic Shift** - fills the newly created bit position with a copy of the number's sign bit ![](https://i.imgur.com/630JGSw.png) #### 7.1.2 SHL Instruction - performs a logical left shift on the destination operand - filling the lowest bit with 0. - the highest bit is moved to the *Carry Flag* ![](https://i.imgur.com/LXXmAV1.png) ```asm= SHL reg,imm8 ; imm8: integer 0-255 SHL mem,imm8 SHL reg,CL SHL mem,CL ``` - Fast Multiplication ![](https://i.imgur.com/r5CJui3.png) #### 7.1.3 SHR Instruction - logical right shift - replace the highest bit with a 0 - the lowest bit is copied to *Carry Flag* ```asm= mov al, 0D0h ; AL = 11010000b shr al, 1 ; AL = 01101000b, CF = 0 ``` #### 7.1.4 SAL and SAR Instructions - **SAL** (shift arithmetic left) is identical to SHL - **SAR** (shift arithmetic right) performs a right arithmetic shift on the destination operand. ![](https://i.imgur.com/LyVJ3A0.png) ```ams= mov dl, -80 sar dl, 1 ;DL = -40 sar dl, 2 ;DL = -10 ``` #### 7.1.5 ROL Instruction - shifts each bit to the left - the highest bit is copied into both the Carry flag and into the lowest bit - No bits are lost ![](https://i.imgur.com/BLUcYyE.png) ```ams= mov al, 11110000b rol al, 1 ;AL = 11100001b mov dl, 3Fh rol al 4 ;DL = F3h ``` #### 7.1.6 ROR Instruction - shifts each bit to the right - The lowest bit is copied into both the Carry flag and into the highest bit - No bits are lost ![](https://i.imgur.com/L4JNknp.png) ```ams= mov al, 11110000b ror al, 1 ;AL = 01111000b mov dl, 3Fh ror dl, 4 ;DL = F3h ``` #### 7.1.7 RCL and RCR Instruction - RCL ![](https://i.imgur.com/hcwl7Ak.png) - RCR ![](https://i.imgur.com/Hu2GfOo.png) #### 7.1.8 Signed Overflow #### 7.1.9 SHLD/SHRD Instructions ```asm= SHLD/SHRD destination, source, count SHLD/SHRD reg16/32, reg16/32, imm8/CL SHLD/SHRD mem16/32, reg16/32, imm8/CL ``` - SHLD ![](https://i.imgur.com/Q9EjKxj.png) - SHRD ![](https://i.imgur.com/hgwsjpL.png) ### 7.2 Shift and Rotate Applications ### 7.3 Multiplication and Division Instructions ```asm= MUL/DIV reg/mem8 MUL/DIV reg/mem16 MUL/DIV reg/mem32 ``` - Multiplication - MUL | Multiplicand | Multiplier | Product | | ------------ | ---------- | ------- | | AL | reg/mem8 | AX | | AX | reg/mem16 | DX:AX | | EAX | reg/mem32 | EDX:EAX | - IMUL - Single-Operand | Multiplicand | Multiplier | Product | | ------------ | ---------- | ------- | | AL | reg/mem8 | AX | | AX | reg/mem16 | DX:AX | | EAX | reg/mem32 | EDX:EAX | - Two-Operand - Three-Operand - Division ![](https://i.imgur.com/lAItDnC.png) - sign Extension Instructions - CBW, CWD, CWD, CDQ convert byte... to word... - DIV for unsigned - IDIV for signed ### 7.4 Extended Addition and Subtraction - Operands are binary values - Same syntax as **ADD**, **SUB**, etc. - operands must be the same size ```asm= ADC/SBB reg, reg/mem/imm ADC/SBB mem, reg, imm ``` #### ADC Instruction - adds both a source operand and the contents of the Carry flag to a destination operand. - example ```asm= mov edx,0 mov eax,0FFFFFFFFh add eax,0FFFFFFFFh ;carry flag = 1 adc edx,0 ;EDX:EAX = 00000001FFFFFFFEh ``` ```asm= mov edx,0 mov eax,0FFFFFFFFh add eax,1 ;carry flag = 1, eax = 00000000h adc edx,0 ;EDX:EAX = 0000000100000000h ``` #### SBB Instruction - The SBB (subtract with borrow) instruction subtracts both a source operand and the value of the Carry flag from a destination operand. ![](https://i.imgur.com/EKa2KuI.png) ### 7.5 ASCII and Unpacked Decimal Arithmetic #### Binary-Coded Decimal (BCD) - use 4 binary bits to represent each decimal digit - A number using unpacked BCD representation stores a decimal digit in the lower four bits of each byte - example : 5,678 stored as the sequence of hexadecimal bytes: ![](https://i.imgur.com/zepVrVT.png) #### ASCII Decimal - A number using ASCII Decimal representation stores a single ASCII digit in each byte - example : 5,678 stored as the sequence of hexadecimal bytes: ![](https://i.imgur.com/1w4Utb5.png) #### AAA Instruction - ASCII adjust after addition - adjusts the binary result of an ADD or ADC instruction - Example add '8' and '2' ```asm= mov ah,0 mov al,'8' ; AX = 0038h add al,'2' ; AX = 006Ah aaa ; AX = 0100h (adjust result) or ax,3030h ; AX = 3130h = '10' ``` #### AAS Instruction - ASCII adjust after subtraction - adjusts the binary result of an SUB or SBB instruction - Example sub '8' from '9' ```asm= mov ah,0 mov al,'8' ; AX = 0038h sub al,'9' ; AX = 00FFh aas ; AX = FF09h, CF=1 or al,30h ; AL = '9' ``` #### AAM Instruction - ASCII adjust after multiplication - adjusts the binary result of a MUL instruction - Example mul '5' and '6' ```asm= mov bl,05h ; first operand mov al,06h ; second operand mul bl ; AX = 001Eh aam ; AX = 0300h or ax,3030h ; AX = 3330h = '30' ``` #### AAD Instruction - ASCII adjust before division - adjusts the unpacked BCD dividend in AX before a division operation - Example ```asm .data quotient BYTE ? remainder BYTE ? .code mov ax,0307h ; dividend aad ; AX = 0025h mov bl,5 ; divisor div bl ; AX = 0207h mov quotient,al mov remainder,ah ``` ### 7.6 Packed Decimal Arithmetic - Packed BCD - Packed decimal integers store two decimal digits per byte - For example, 12,345,678 can be stored as the following sequence of hexadecimal bytes:![](https://i.imgur.com/khNeNWC.png) #### DAA Instruction - decimal adjust after addition - converts the binary result of an ADD or ADC - If the lower digit is adjusted, the **Auxiliary Carry flag** is set. - If the upper digit is adjusted, the **Carry flag** is set - Example: calculate BCD $35+48$ ```asm= mov al,35h add al,48h ; AL = 7Dh daa ; AL = 83h, CF = 0 ``` - Example: calculate BCD $35+65$ ```asm= mov al,35h add al,65h ; AL = 9Ah daa ; AL = 00h, CF = 1 ``` - Example: calculate BCD $69+29$ ```asm= mov al,69h add al,29h ; AL = 92h daa ; AL = 98h, CF = 0 ``` #### DAS Instruction - decinal adjust after subtraction - converts the binary result of an SUB or SBB ```asm= mov bl, 48h mov al, 85h sub al, bl ; AL = 3Dh das ; AL = 37h (adjusted result) ``` ## Ch.08 Advanced Procedures ## Ch.09 Strings and Arrays | 指令 | 描述 | 對ESI跟EDI的影響 | |:----:|:------------:|:----------------:| | CLD | 清除方向旗幟 | 遞增 | | STD | 設定方向旗幟 | 遞減 | ### 9.2 String Primitive Instructions | Instructions | Description | | ------------------- | ------------------------------------------------------------------------------------------------- | | MOVSB, MOVSW, MOBSD | **copy data** from the memory location pointed to by ESI to the memory location pointed to by EDI | | CMPSB, CMPSW, CMPSD | **compare** a memory operand pointed to by ESI to a memory operand pointed to by EDI | | SCASB, SCASW, SCASD | **compare** a value in AL/AX/EAX to a byte, word, or doubleword, respectively, addressed by EDI | | STOSB, STOSW, STOSD | store the contents of AL/AX/EAX, respectively, in memory at the offset pointed to by EDI | | LODSB, LODSW, LODSD | load a byte or word from memory at ESI into AL/AX/EAX, respectively | ## Ch.10 Structures and Macros ## Ch.12 Floating-Point Processing and Instruction Encoding ## 考古題 ### 2019 1. ```ams= include irvine32.inc .data monstr byte 5 dup (?), 0dh, 0ah, 0 wktb byte "jandebmaraplmayjunjulaugsepoctnovdec" .code main proc call readhex mov dx,ax shr dx,5 and dx,0fh dec dl mov al, 3 mul dl mov esi, offset wktb and eax, 0ffffh add esi, eax mov edi, offset monstr mov ecx, 3 cld rep movsb mov edx, offset monstr call writestring exit main endp end main ``` 2. 老大版 ```asm= include irvine32.inc .data TDATA BYTE "This is a book.",0 SCHAR BYTE 's' .code main proc mov edi, offset tdata mov al, schar mov ecx, lengthof tdata l3: repnz scasb jnz l1 dec edi cmp byte ptr[edi],'a' jb l2 cmp byte ptr[edi],'Z' ja l2 and [edi], 0dfh l2: inc edi jmp l3 l1: mov edx, offset monstr call writestring exit main endp end main ``` S版 ```asm= INCLUDE Irvine32.inc .data TDATA BYTE "ThiS is a book.",0 SCHAR BYTE 'k' .code main PROC mov al, schar mov ecx, sizeof tdata mov edi, offset tdata L1: repne scasb jne L2 mov dl, [edi-1] sub dl, 20h mov [edi-1], dl jmp L1 L2: mov edx, offset tdata call writestring exit main ENDP END main ``` 3. 吳老大 ```asm= include irvine32.inc .data AUGEND BYTE 4 DUP (?) ADDEND BYTE 4 DUP (?) SUN BYTE 5 DUP (?) .code main proc call readhex mov dword ptr augend, eax call readhex mov dword ptr addend, eax mov ax, word ptr augend add ax, word ptr addend mov word ptr sum, ax mov ax, word ptr augend+2 adc ax, word ptr addend+2 mov word ptr sum+2, ax mov al, 0 adc al, 0 mov sum+4, al call crlf mov eax, dword ptr sum call writehex exit main endp end main ``` 32位元加16位元版 ```asm= .data AUGEND WORD 1111h,2323h,1919h,0ffffh ADDEND WORD 0101h,5050h,1a1ah,0ffffh SUM WORD 5 DUP (?) .code main PROC mov esi, 0 mov ecx, 4 mov dx, 0 clc L1: mov ax, augend[si] mov bx, addend[si] add ax, dx add ax, bx adc dx, 0 mov sum[esi], ax add esi, type word loop L1 mov sum[esi], dx mov esi, 0 mov ecx, 5 L2: movzx eax, sum[esi] call WriteHex add esi, type word loop L2 call crlf exit main ENDP END main ``` 4. ```ams= include irvine32.inc .data NROW BYTE 3 NCOL BYTE 3 ARDAT DWORD 100 DUP (1,2,3,4) ROWNO BYTE ? COLNO BYTE ? .code main proc call readhex mov rowno, al call readhex mov colno, al mov esi, offset ardat mov al, rowno mov bl, ncol mul bl and eax, offffh movzx ebx, colno add eax, ebx mov eax, [esi+eax*4] call writedec exit main endp end main ``` 5. ```asm= include irvine32.inc .data STUDENTS BYTE 100 DUP (10 DUP (?), 10 DUP (?), ?) SNAME BYTE 10 DUP (?) NOSTUD BYTE ? SCORE BYTE ? .code main proc mov esi, offset students+10 mov edi, offset sname movzx ecx, nostud l1: push ecx mov edi, offset sname mov ecx, 10 push esi repz cmpsb jz l2 pop esi add esi, 21 pop ecx loop l1 clc mov score, 0 jmp l3 l2: stc mov al, [esi] mov sccore, al l3: exit main endp end main ``` 105 1. ```masm= INCLUDE IRVINE32.INC .data expon BYTE 10 dup (?) .code main proc call ReadHex mov ebx,eax shr ebx, 23 mov esi,OFFSET expon sub bl,127 jnsL1 mov BYTE PTR[esi],'-' inc esi neg bl L1: movzx ax,bl mov bl,10 mov ecx,0 L2: div bl push edx inc ecx cmp ax,0 jnz l2 L3: pop eax add al,30h mov [esi],al inc esi loop L3 mov byte ptr [esi], 0 mov edx,OFFSET expon call WriteString exit main endp end main ``` 2. ```masm= .data ``` 3. ```asm= .data DEC1 BYTE "1000", 0 DEC2 BYTE "0023", 0 DEC3 BYTE "00000", 0 .code main proc mov esi, offset dec1+3 mov edi, offset dec2+3 mov ebx, offset dec3+4 mov ecx, 4 clc l1: mov al, [esi] adc al, [edi] aaa mov [ebx], al dec esi dec edi dec ebx loop l1 mov a1, 0 adc al, 0 mov [ebx], al mov edx, offset dec3 mov ecx,5 l2: mov al, [edx] add al, 30h call writechar inc edx loop l2 exit main endp end main ``` 4. ```amsm= .data STR1 BYTE "CSUE NTNMUABC...1",0 SLEN BYTE 10 CHARDEL BYTE 'M' .code main proc mov edi, offset str1 mov al, chardel movzx ecx, slen repnz scasb jnz l1 mov esi,edi dec edi rep movsb dec slen l1: mov edx, offset str1 call writestring exit main endp main end ``` 5. ```asm= .data stud BYTE 100 dup (10 dup ('A'), 10 dup ('1'), 3 dup (90), ?) Stno BYTE 17 .code main proc mov edi, offset stud+20 movzx ecx, stno l1: movzx ax, [edi] movzx bx, [edi+1] add ax, bx movzx bx, [edi+2] add ax, bx mov bl, 3 div bl mov [edi+3],al add edi, 24 loop l1 movzx ecx,stno mov esi,OFFSET stud+23 L2: movxz eax,BYTE PTR call writestring exit main endp end main ``` ### 某年 2. ```asm= .data X dword 10 Y dword 20 Z dword ? .code main proc PUSH offset X push offset y push offset z call proc1 mov eax, z call writedec call crlf exit main endp proc1 proc push eax push ebx push esi mov esi, [esp+24] mov eax, [esi] mov esi, [esp+20] add eax, [esi] mov esi, [esp+16] mov [esi], eax pop esi pop ebx pop eax ret 12 proc1 endp ```