Skip to content

Commit f08bc5b

Browse files
committed
Add informations about Assembly interface
1 parent ee6f799 commit f08bc5b

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Chapter-5/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,44 @@ void Io::print(const char *s, ...){
148148
return;
149149
}
150150
```
151+
152+
#### Assembly interface
153+
154+
A large number of instructions are available in Assembly but there is not equivalent in C (like cli, sti, in and out), so we need an interface to these instructions.
155+
156+
In C, we can include Assembly using the diretctive "asm()", gcc use gas to compile the assembly.
157+
158+
**Caution:** gas use the AT&T syntax.
159+
160+
```cpp
161+
/* output byte */
162+
void Io::outb(u32 ad, u8 v){
163+
asmv("outb %%al, %%dx" :: "d" (ad), "a" (v));;
164+
}
165+
/* output word */
166+
void Io::outw(u32 ad, u16 v){
167+
asmv("outw %%ax, %%dx" :: "d" (ad), "a" (v));
168+
}
169+
/* output word */
170+
void Io::outl(u32 ad, u32 v){
171+
asmv("outl %%eax, %%dx" : : "d" (ad), "a" (v));
172+
}
173+
/* input byte */
174+
u8 Io::inb(u32 ad){
175+
u8 _v; \
176+
asmv("inb %%dx, %%al" : "=a" (_v) : "d" (ad)); \
177+
return _v;
178+
}
179+
/* input word */
180+
u16 Io::inw(u32 ad){
181+
u16 _v; \
182+
asmv("inw %%dx, %%ax" : "=a" (_v) : "d" (ad)); \
183+
return _v;
184+
}
185+
/* input word */
186+
u32 Io::inl(u32 ad){
187+
u32 _v; \
188+
asmv("inl %%dx, %%eax" : "=a" (_v) : "d" (ad)); \
189+
return _v;
190+
}
191+
```

0 commit comments

Comments
 (0)