All articles
รวมเอกสาร CSJan 28, 20230 min read

Computer Organization

งง นอน

A

Athicha Leksansern

Full-stack Engineer

How to Assembly by Tonkaew

Creating Label

int func1() {

}
func1:

movq

  • movq src, dest

example.

mov $6, %rdi
; regis rdi = 6

mov $0x4000, %rdi
; regis rdi = 0x4000
mov $6, (%rdi)
; meme mem[0x4000] = 6
  • Type of movq

    • 5(%rdi) = mem[%rdi + 5]
    • (%rdi, %rsi) = mem[%rdi + %rsi]
    • (%rax, %rdi, %rsi) = mem[%rax + (%rdi * %rsi)]
    • (%rax, %rdi, 4) = mem[%rax + (4 * %rdi)]
    • 5(,%rdi,%rsi) = mem[5 + (%rdi * %rsi)]
  • movl (long)

  • movq (words)

Arithmatics instruction

  • adds src, dest ; dest = dest + src

example.

; %rdi = 5
; %rsi = 4
addq %rdi, %rsi
; %rsi = 4 + 5 = 9
  • subq src, dest
  • imulq src, dest
  • salq: Shift Arithmatic Left
  • shlq: Shift Logic Left
  • sarq: Shift Arithmatic Right
  • shrq: Shift Logic Left
  • xorq src, dest
  • andq src, dest
  • orq src, dest
  • incq dest ; increase, dest = dest + 1
  • decq dest ; decrese, dest = dest - 1
  • negq dest ; negative, dest = -1 * dest
  • notq dest ; not, dest = ~dest

Convert C <=> Assembly

int fun1(int a) {
    return a;
}
fun1:
    movs %rdi, %rax
int fun2(int a, int b) {
    return a + b;
}
fun2:
  movs %rdi, %rax ; return = a
  adds %rsi, %rax ; return = a + b
  • jmp
.l1:  
    ...
    jmp .L2
    ..
    ..
.l2:
    ...
.final:
    ...

Condition flags

  • ZF Zero
  • OF Overflow
  • SF Signed
  • CF Carry

testq

  • testq src2, src1
  • src2 & src1 == 0 ZF = 1
  • src2 & src1 < 0 SF 1

example.

; %rdi = 0
testq %rdi, %rdi
; ZF 1
; SF 0

; %rdi = 1
testq %rdi, %rdi
; ZF 0
; SF 0

adds 0b1111 + 0b0001 0b0000 CF = 1 ZF = 0

cmpq

  • cmpq a, b example.
cmpq a, b
je ; jump if a == b
jne ; jump if a != b
js ; jump if (b - a) is negative
jns ; jump if (b - a) is not negative
jg ; jump if b > a
jge ; jump if b >= a
jl ; jump if b < a
jle ; jump if b <= a
int func3(int a, int b) {
    if(a > b) { // b < a
        return a; // L1
    }
    else {
        return b; // L2
    }
}
func3:
    cmpq %rdi, %rsi ; compare a, b
    jl L1
L2:
    movs %rsi, %rax
    jmp finally
L1:
    movs %rdi, %rax
finally:
    ...
    movq (%rdi), %rax ; %rax = mem[%rdi]
    leaq %rax, (%rdi) ; mem[%rdi] = %rax
    ; rdi = 0x4000 0000
    movq 4(%rdi), %rax ; %rax = mem[0x4000 0004]
    laeq 4(%rdi), %rax ; %rax = 0x4000 0004
// sums n * (n + 1) / 2
int func1(int a) {
    int b = 0;
    while(a != 0) {
        a -= 1;
        b += a;
    }
    return b;
}
func1:
    movq $0, %rbx ; %rbx = 0 = b
    movq %rbx, %rax ; %rax = b = 0 ; not exists in C
loop:
    testq %rdi, %rdi ; test a
    je endLoop ; if a == 0, jump to endLoop
    subq $1, %rdi ; %rdi -= 1; a -= 1;
    addq %rdi, %rax ; %rax += %rdi; b += a;
    jmp loop
endLoop:
    ret

Stacks %rsp: stack pointer ,%rbp: base pointer

  • pushq ; push into stacks
  • popq ; pop out off stacks
  • call; call function, push current pc + 1 intp stacks
  • ret ; pop, and set pc to that data

Branch / Jump

  • jmp: jump without condition
  • je: jump equal or zero
  • jne: jump not equal or not zero
  • js: jump negative (signed)
  • jns: jump not negative (signed)
  • jg: jump greater
  • jge: jump greater or equal
  • jl: jump less
  • jle: jump less or equal
  • ja: jump above (~CF & ~ZF)
  • jb: jump below (CF)

อื่นๆ

  • ส่วนของ Computer
    • CPU
      • ALU
      • Bus
      • Cache
      • Register
    • Memory

function in C, arguments (int a, int b, ...); -> %rdi %rsi %rdx %rcx %r8 %r9

in c return x; in asm %rax

Back to cscourse