{"id":449,"date":"2018-04-16T10:00:00","date_gmt":"2018-04-16T01:00:00","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2018\/04\/16\/introduce-assembly"},"modified":"2018-04-16T10:00:00","modified_gmt":"2018-04-16T01:00:00","slug":"introduce-assembly","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2018\/04\/16\/introduce-assembly\/","title":{"rendered":"\u30a2\u30bb\u30f3\u30d6\u30ea\u8a00\u8a9e\u306b\u5165\u9580\u3057\u3088\u3046"},"content":{"rendered":"
MMM\u30d0\u30c3\u30af\u30a8\u30f3\u30c9\u30a8\u30f3\u30b8\u30cb\u30a2\u306e\u67f3\u6cbc\u3067\u3059\u3002
\n\u3044\u3088\u3044\u3088\u4eca\u9031\u306fJapan Container Days\u3067\u3059\u306d!!<\/p>\n
\u4eca\u56de\u306f\u3001Hello World\u3092\u51fa\u529b\u3057\u3066\u307f\u308b\u3053\u3068\u3067\u3001
\n\u30a2\u30bb\u30f3\u30d6\u30ea\u8a00\u8a9e\u306e\u57fa\u790e\u3092\u5b66\u3093\u3067\u307f\u3088\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n
\u4eca\u56de\u306f\u30a2\u30bb\u30f3\u30d6\u30e9\u306bnasm\u3092\u63a1\u7528\u3057\u307e\u3059\u3002
\n\u30d0\u30fc\u30b8\u30e7\u30f3\u306f2.0.0\u4ee5\u4e0a\u3067\u3042\u308b\u3053\u3068\u3092\u63a8\u5968\u3057\u307e\u3059\u3002<\/p>\n
\u7b46\u8005\u306e\u74b0\u5883\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n
$ nasm --version\nNASM version 2.13.01 compiled on Feb 28 2018<\/code><\/pre>\n\u307e\u305f\u3001\u7b46\u8005\u306e\u30de\u30b7\u30f3\u306eCPU\u60c5\u5831\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059\u3002(4\u30b3\u30a2\u306a\u305f\u30814\u884c\u8868\u793a\u3055\u308c\u3066\u3044\u307e\u3059\u3002)<\/p>\n
$ cat \/proc\/cpuinfo | grep 'model name'\nmodel name : Intel(R) Core(TM) i3-6006U CPU @ 2.00GHz\nmodel name : Intel(R) Core(TM) i3-6006U CPU @ 2.00GHz\nmodel name : Intel(R) Core(TM) i3-6006U CPU @ 2.00GHz\nmodel name : Intel(R) Core(TM) i3-6006U CPU @ 2.00GHz<\/code><\/pre>\nLinux\u30c7\u30a3\u30b9\u30c8\u30ea\u30d3\u30e5\u30fc\u30b7\u30e7\u30f3\u306fGentoo\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002(darwin\u3067\u3082\u305f\u3076\u3093\u3067\u304d\u308b\u3068\u601d\u3046\u306e\u3067\u9811\u5f35\u3063\u3066\u304f\u3060\u3055\u3044\u3002)
\n\u30c7\u30a3\u30b9\u30c8\u30ea\u53ca\u3073\u30ab\u30fc\u30cd\u30eb\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n
$ cat \/etc\/gentoo-release\nGentoo Base System release 2.4.1\n\n$ uname -r\n4.14.8-gentoo-r1<\/code><\/pre>\n\u30bb\u30af\u30b7\u30e7\u30f3<\/h2>\n
\u672c\u30a8\u30f3\u30c8\u30ea\u3067\u306f\u30a2\u30bb\u30f3\u30d6\u30ea\u306e\u5168\u3066\u306e\u6587\u6cd5\u306b\u306f\u89e6\u308c\u307e\u305b\u3093\u3002
\n\u3068\u308a\u3042\u3048\u305aHello World\u306e\u51fa\u529b\u306b\u5fc5\u8981\u306a\u77e5\u8b58\u306b\u7d5e\u3063\u3066\u8aac\u660e\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n
\u4eca\u56de\u306f\u3001 \u30c7\u30fc\u30bf\u30bb\u30af\u30b7\u30e7\u30f3<\/code> \u3068 \u30c6\u30ad\u30b9\u30c8\u30bb\u30af\u30b7\u30e7\u30f3<\/code> \u306b\u3064\u3044\u3066\u8aac\u660e\u3057\u307e\u3059\u3002<\/p>\n\u30c7\u30fc\u30bf\u30bb\u30af\u30b7\u30e7\u30f3<\/h3>\n
\u30c7\u30fc\u30bf\u30bb\u30af\u30b7\u30e7\u30f3\u306f\u3001\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u66f8\u304d\u65b9\u3092\u3057\u307e\u3059\u3002<\/p>\n
section .data\n msg db "Hello world!"<\/code><\/pre>\nmsg<\/code> \u306f\u5909\u6570\u540d\u3001 "Hello world!"<\/code> \u304c\u30c7\u30fc\u30bf\u3067\u3059\u3002
\ndb<\/code> \u306f\u30011\u30d0\u30a4\u30c8\u5206\u306e\u30e1\u30e2\u30ea\u3092\u78ba\u4fdd\u3059\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002(dw<\/code>\u306f2\u30d0\u30a4\u30c8\u3001dd<\/code>\u306f4\u30d0\u30a4\u30c8)<\/p>\n\u3064\u307e\u308a\u3001\u4e0a\u8a18\u306f<\/p>\n
char msg[] = "Hello, world!"<\/code><\/pre>\n\u3068\u540c\u3058\u3088\u3046\u306a\u3053\u3068\u3067\u3059\u3002<\/p>\n
\u30c6\u30ad\u30b9\u30c8\u30bb\u30af\u30b7\u30e7\u30f3<\/h3>\n
\u30c6\u30ad\u30b9\u30c8\u30bb\u30af\u30b7\u30e7\u30f3\u306b\u306f\u3001\u5b9f\u969b\u306e\u51e6\u7406\u3092\u8a18\u8ff0\u3057\u307e\u3059\u3002
\n\u30c6\u30ad\u30b9\u30c8\u30bb\u30af\u30b7\u30e7\u30f3\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u958b\u59cb\u3057\u307e\u3059\u3002<\/p>\n
section .text\n global _start<\/code><\/pre>\nglobal _start<\/code> \u306f\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u30dd\u30a4\u30f3\u30c8\u306b\u306a\u308a\u307e\u3059\u3002
\n_start<\/code> \u306f\u30e9\u30d9\u30eb(\u95a2\u6570\u306e\u958b\u59cb\u4f4d\u7f6e\u307f\u305f\u3044\u306a\u3082\u306e)\u3068\u3057\u3066\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n\u51e6\u7406\u306e\u6587\u6cd5\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n
[label:] instruction [operands] [; comment]<\/code><\/pre>\n\u4f8b\u3048\u3070\u3001<\/p>\n
mov rax, 1<\/code><\/pre>\n\u3068\u66f8\u3051\u3070\u3001 rax<\/code> \u30ec\u30b8\u30b9\u30bf\u306b 1<\/code> \u3092\u30bb\u30c3\u30c8\u3059\u308b\u3001\u3068\u3044\u3046\u610f\u5473\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\u30ec\u30b8\u30b9\u30bf<\/h3>\n
\u30ec\u30b8\u30b9\u30bf\u3068\u306f\u8981\u3059\u308b\u306b\u8a18\u61b6\u9818\u57df\u3067\u3059\u3002
\n\u30e1\u30e2\u30ea\u306e\u3088\u3046\u306b\u3001\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002
\n\u30e1\u30e2\u30ea\u3088\u308a\u3082\u9ad8\u901f\u306b\u52d5\u4f5c\u3057\u3001CPU\u306e\u4e0a(\u7269\u7406)\u306b\u3042\u308a\u307e\u3059\u3002
\n\u672c\u30a8\u30f3\u30c8\u30ea\u3067\u767b\u5834\u3059\u308b\u30ec\u30b8\u30b9\u30bf\u306f\u3001x64\u74b0\u5883\u306e\u3082\u306e\u3067\u3042\u308b\u3053\u3068\u306b\u7559\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002(x86\u3068\u304b\u3060\u3068\u540d\u524d\u304c\u9055\u3044\u307e\u3059\u3002)<\/p>\n
\u30ec\u30b8\u30b9\u30bf\u306f(\u7269\u7406\u7684\u306b)\u8907\u6570\u5b58\u5728\u3057\u3066\u304a\u308a\u3001\u305d\u308c\u305e\u308c\u540d\u524d\u304c\u3064\u3044\u3066\u3044\u307e\u3059\u3002
\n\u4eca\u56de\u4f7f\u7528\u3059\u308b\u306e\u306f\u4ee5\u4e0b\u306e\u30ec\u30b8\u30b9\u30bf\u3067\u3059\u3002<\/p>\n
\n- rax : \u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u756a\u53f7\u3092\u4fdd\u5b58\u3059\u308b<\/li>\n
- rdi : \u7b2c1\u5f15\u6570\u3092\u4fdd\u5b58\u3059\u308b<\/li>\n
- rsi : \u7b2c2\u5f15\u6570(\u306e\u30dd\u30a4\u30f3\u30bf)\u3092\u4fdd\u5b58\u3059\u308b<\/li>\n
- rdx : \u7b2c3\u5f15\u6570\u3092\u4fdd\u5b58\u3059\u308b<\/li>\n<\/ul>\n
\u3068\u308a\u3042\u3048\u305a\u3053\u308c\u3060\u3051\u308f\u304b\u308c\u3070\u3001Hello World\u306f\u3067\u304d\u307e\u3059\u3002\u30b3\u30fc\u30c9\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n
\u30a2\u30bb\u30f3\u30d6\u30ea\u3067Hello World<\/h2>\n
\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30b3\u30fc\u30c9\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n
section .data\n msg db "Hello world!"\n\nsection .text\n global _start\n_start:\n mov rax, 1\n mov rdi, 1\n mov rsi, msg\n mov rdx, 12\n syscall\n mov rax, 60\n mov rdi, 0\n syscall<\/code><\/pre>\n\u30c7\u30fc\u30bf\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u6587\u5b57\u5217\u3092\u5ba3\u8a00\u3057\u3066\u3044\u308b\u3060\u3051\u3067\u3059\u3002
\n_start<\/code> \u3092\u8aad\u3093\u3067\u3044\u304d\u307e\u3059\u3002<\/p>\nmov rax, 1<\/code><\/pre>\n\u306f\u3001rax\u306b1\u3092\u5165\u308c\u3066\u3044\u307e\u3059\u3002
\n\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u756a\u53f71\u306f sys_write<\/code> \u3067\u3059\u306d\u3002
\n\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u756a\u53f7\u306fLinux\u30ab\u30fc\u30cd\u30eb\u306esyscall_64.tbl<\/a>\u304b\u3089\u53c2\u7167\u3067\u304d\u307e\u3059\u3002(\u4fbf\u5229)<\/p>\nmov rdi, 1\nmov rsi, msg\nmov rdx, 12<\/code><\/pre>\n\u306b\u3064\u3044\u3066\u306f\u3001\u524d\u8ff0\u306e\u901a\u308a\u3001\u5f15\u6570\u3092\u8a70\u3081\u3066\u3044\u307e\u3059\u3002
\nsys_write<\/code> \u306e\u5f15\u6570\u3092\u77e5\u308b\u305f\u3081\u306b\u3001man\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002 (man 2 write<\/code>)<\/p>\nWRITE(2) Linux Programmer's Manual WRITE(2)\n\nNAME\n write - write to a file descriptor\n\nSYNOPSIS\n #include <unistd.h>\n\n ssize_t write(int fd, const void *buf, size_t count);<\/code><\/pre>\n\u5f15\u6570\u304c3\u3064\u5fc5\u8981\u306a\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002
\nfd<\/code> \u306f\u30d5\u30a1\u30a4\u30eb\u30c7\u30a3\u30b9\u30af\u30ea\u30d7\u30bf\u3067\u3059\u3002\u6a19\u6e96\u51fa\u529b\u3057\u305f\u3044\u306e\u3067\u30011\u3092\u30bb\u30c3\u30c8\u3057\u307e\u3059\u3002
\nbuf<\/code> \u306b\u306f\u6587\u5b57\u5217\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002
\ncount<\/code> \u306b\u306f\u3001\u30c7\u30fc\u30bf\u306e\u30b5\u30a4\u30ba\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\nsyscall<\/code><\/pre>\n\u4e0a\u8a18\u3067\u3001\u4eca\u306e\u30ec\u30b8\u30b9\u30bf\u306e\u72b6\u614b\u3092\u57fa\u306b\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u3092\u767a\u884c\u3057\u307e\u3059\u3002
\n\u3064\u307e\u308a\u3001\u4e0a\u8a183\u3064\u306e\u5f15\u6570\u3092\u57fa\u306b\u3001rax\u30ec\u30b8\u30b9\u30bf\u306b\u8a70\u3081\u3066\u3044\u308b\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb(sys_write)\u3092\u767a\u884c\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n
mov rax, 60\nmov rdi, 0\nsyscall<\/code><\/pre>\n\u3053\u3061\u3089\u306f\u3001<\/p>\n
exit(0)<\/code><\/pre>\n\u3092\u884c\u3063\u3066\u3044\u307e\u3059\u3002
\n\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306e60\u756a\u306fexit\u3067\u3001\u5f15\u6570\u306f\u7d42\u4e86\u30b3\u30fc\u30c9\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n
\u5b9f\u884c\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u884c\u3044\u307e\u3059\u3002<\/p>\n
$ nasm -f elf64 -o hello.o hello.asm # \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30d5\u30a1\u30a4\u30eb\u751f\u6210\n$ ld -o hello hello.o # \u5b9f\u884c\u30d0\u30a4\u30ca\u30ea\u751f\u6210\n$ .\/hello # \u5b9f\u884c<\/code><\/pre>\n\u7121\u4e8b Hello World!<\/code> \u304c\u8868\u793a\u3055\u308c\u308c\u3070\u52dd\u3061\u3067\u3059\u3002\u3084\u308a\u307e\u3057\u305f\u306d\u3002<\/p>\n\u307e\u3068\u3081<\/h2>\n
Hello World\u3092\u901a\u3057\u3066\u3001\u30ec\u30b8\u30b9\u30bf\u306e\u4f7f\u3044\u65b9\u3001\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u767a\u884c\u306e\u6d41\u308c\u3092\u8ffd\u3063\u3066\u307f\u307e\u3057\u305f\u3002
\n\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306b\u3064\u3044\u3066\u306f\u3001\u3053\u3046\u3044\u3046\u30b5\u30a4\u30c8<\/a>\u3092\u4f7f\u3048\u3070
\n\u4f7f\u3046\u3079\u304d\u30ec\u30b8\u30b9\u30bf\u3082\u6559\u3048\u3066\u304f\u308c\u308b\u306e\u3067\u4fbf\u5229\u3067\u3059\u3002<\/p>\n