aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/jmp.rs
blob: 5f430acdfac9668a79012abb238f4fae34926ee4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use juicebox_asm::insn::Jmp;
use juicebox_asm::{Asm, Label};

#[test]
#[should_panic]
fn unbound_label() {
    let _l = Label::new();
}

#[test]
#[should_panic]
fn unbound_label2() {
    let mut lbl = Label::new();
    let mut asm = Asm::new();
    asm.jmp(&mut lbl);
}

#[test]
fn jmp_label() {
    {
        // Bind first.
        let mut lbl = Label::new();
        let mut asm = Asm::new();
        asm.bind(&mut lbl);
        asm.jmp(&mut lbl);
        // 0xfffffffb -> -5
        assert_eq!(asm.into_code(), [0xe9, 0xfb, 0xff, 0xff, 0xff]);
    }
    {
        // Bind later.
        let mut lbl = Label::new();
        let mut asm = Asm::new();
        asm.jmp(&mut lbl);
        asm.bind(&mut lbl);
        assert_eq!(asm.into_code(), [0xe9, 0x00, 0x00, 0x00, 0x00]);
    }
}

#[test]
fn jmp_label2() {
    {
        let mut lbl = Label::new();
        let mut asm = Asm::new();
        asm.jmp(&mut lbl);
        asm.nop();
        asm.nop();
        asm.bind(&mut lbl);
        assert_eq!(asm.into_code(), [0xe9, 0x02, 0x00, 0x00, 0x00, 0x90, 0x90]);
    }
    {
        let mut lbl = Label::new();
        let mut asm = Asm::new();
        asm.jmp(&mut lbl);
        for _ in 0..0x1ff {
            asm.nop();
        }
        asm.bind(&mut lbl);
        assert_eq!(asm.into_code()[..5], [0xe9, 0xff, 0x01, 0x00, 0x00]);
    }
}