Assembly Language
final

NTNU 組合語言

Back to Note Overview
Back to Assembly Language
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
      .
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
  • Arithmetic Shift
    • fills the newly created bit position with a copy of the number's sign bit
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

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
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
SHL reg,imm8 ; imm8: integer 0-255 SHL mem,imm8 SHL reg,CL SHL mem,CL
  • Fast Multiplication
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

7.1.3 SHR Instruction

  • logical right shift
  • replace the highest bit with a 0
  • the lowest bit is copied to Carry Flag
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.
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
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
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
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
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
mov al, 11110000b ror al, 1 ;AL = 01111000b mov dl, 3Fh ror dl, 4 ;DL = F3h

7.1.7 RCL and RCR Instruction

  • RCL
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  • RCR
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

7.1.8 Signed Overflow

7.1.9 SHLD/SHRD Instructions

SHLD/SHRD destination, source, count SHLD/SHRD reg16/32, reg16/32, imm8/CL SHLD/SHRD mem16/32, reg16/32, imm8/CL
  • SHLD
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  • SHRD
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

7.2 Shift and Rotate Applications

7.3 Multiplication and Division Instructions

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

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    • 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
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
mov edx,0 mov eax,0FFFFFFFFh add eax,0FFFFFFFFh ;carry flag = 1 adc edx,0 ;EDX:EAX = 00000001FFFFFFFEh
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.

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:

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:

AAA Instruction

  • ASCII adjust after addition
  • adjusts the binary result of an ADD or ADC instruction
  • Example add '8' and '2'
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'
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'
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
.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:

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
mov al,35h add al,48h ; AL = 7Dh daa ; AL = 83h, CF = 0
  • Example: calculate BCD
    35+65
mov al,35h add al,65h ; AL = 9Ah daa ; AL = 00h, CF = 1
  • Example: calculate BCD
    69+29
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
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

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

老大版

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版

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

吳老大

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位元版

.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
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
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.

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
.data
.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
.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
.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

某年

.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