3 ;;; Mes --- Maxwell Equations of Software
4 ;;; Copyright © 2016,2017 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;; This file is part of Mes.
8 ;;; Mes is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; Mes is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with Mes. If not, see <http://www.gnu.org/licenses/>.
23 ;;; libc-i386.mes defines C library routines
27 (define (i386:function-preamble)
29 #x89 #xe5)) ; mov %esp,%ebp
31 (define (i386:function-locals)
32 '(#x83 #xec #x10)) ; sub $0x10,%esp -- 4 local vars
34 ;; (define (i386:formal i n)
36 ;; ((0) (list #x8b #x5d (* (- n 2) 4))) ; mov $00(%ebp),%ebx
37 ;; ((1) (list #x8b #x4d (* (- n 3) 4))) ; mov $00(%ebp),%ecx
38 ;; ((2) (list #x8b #x55 (* (- n 4) 4))) ; mov $00(%ebp),%edx
39 ;; ((3) (list #x8b #x45 (* (- n 5) 4))))) ; mov $00(%ebp),%eax FIXME
41 (define (i386:ref-global o)
42 `(#x68 ,@(int->bv32 o))) ; push $0x<o>
44 (define (i386:ref-local n)
45 `(#xff #x75 ,(- 0 (* 4 n)))) ; pushl 0x<n>(%ebp)
47 (define (i386:push-accu)
50 (define (i386:push-arg s t d)
53 `(#x68 ,@(int->bv32 o))) ; push $<o>
55 ((procedure? o) (o s t d)))))
57 (define (i386:ret . rest)
60 ,@(cond ((null? rest) '())
63 ,@(int->bv32 (car rest))))
64 ((pair? (car rest)) (car rest))
65 ((procedure? (car rest))
71 (define (i386:accu->local n)
72 `(#x89 #x45 ,(- 0 (* 4 n)))) ; mov ,%eax,-<0xn>(%ebp)
74 (define (i386:local->accu n)
75 `(#x8b #x45 ,(- 0 (* 4 n)))) ; mov -<0xn>(%ebp),%eax
77 (define (i386:local->base n)
78 `(#x8b #x55 ,(- 0 (* 4 n)))) ; mov -<0xn>(%ebp),%edx
80 (define (i386:mem-byte->accu)
81 '(#x01 #xd0 ; add %edx,%eax
82 #x0f #xb6 #x00)) ; movzbl (%eax),%eax
84 (define (i386:local-add n v)
85 `(#x83 #x45 ,(- 0 (* 4 n)) ,v)) ; addl $<v>,0x<n>(%ebp)
87 (define (i386:local-assign n v)
88 `(#xc7 #x45 ,(- 0 (* 4 n)) ; movl $<v>,0x<n>(%ebp)
91 (define (i386:ret-local n)
93 #x89 #x45 ,(- 0 (* 4 n)) ; mov %eax,-0x<n>(%ebp)
96 (define (i386:call s t d address . arguments)
97 (let* ((pushes (append-map (i386:push-arg s t d) arguments))
99 (n (length arguments)))
102 #xe8 ,@(int->bv32 (- address 5 s)) ; call relative
103 #x83 #xc4 ,(* n 4) ; add $00,%esp
106 (define (i386:exit s t d)
110 #xb8 #x01 #x00 #x00 #x00 ; mov $0x1,%eax
111 #xcd #x80 ; int $0x80
114 ;; (define (i386:_start s t d)
118 ;; #x89 #xe5 ; mov %esp,%ebp
120 ;; ;;#x83 #xec #x10 ; sub $0x10,%esp -- 4 local vars
122 ;; #xe8 ,@(int->bv32 (- address 5 s)) ; call relative
124 ;; #xb8 #x04 #x00 #x00 #x00 ; mov $0x4,%eax
125 ;; #xcd #x80 ; int $0x80
130 ;; (text-list (text->list t))
131 ;; (statement-offset (- (+ (length prefix) (length text-list))))
132 ;; (address (+ t (function-offset "main" s))))))
134 (define (i386:write s t d)
137 #x89 #xe5 ; mov %esp,%ebp
139 #x8b #x5d #x10 ; mov $0x8(%ebp),%ebx
140 #x8b #x4d #x0c ; mov $0xc(%ebp),%ecx
141 #x8b #x55 #x08 ; mov $0x4(%ebp),%edx
143 #xb8 #x04 #x00 #x00 #x00 ; mov $0x4,%eax
144 #xcd #x80 ; int $0x80
150 (define (i386:jump n)
151 `(#xeb ,(if (>= n 0) n (- n 2)))) ; jmp <n>
153 (define (i386:test-jump n)
154 `(#x84 #xc0 ; test %al,%al
155 #x75 ,(if (>= n 0) n (- n 4)))) ; jne <n>
159 strcmp (char const* a, char const* b)
161 while (*a && *b && *a == *b) {*a++;b++;}
165 8048150: 55 push %ebp
166 8048151: 89 e5 mov %esp,%ebp
167 8048153: eb 0d jmp 8048162 <strcmp+0x12>
170 8048155: 8b 45 08 mov 0x8(%ebp),%eax
171 8048158: 83 c0 01 add $0x1,%eax
172 804815b: 89 45 08 mov %eax,0x8(%ebp)
173 804815e: 83 45 0c 01 addl $0x1,0xc(%ebp)
176 8048162: 8b 45 08 mov 0x8(%ebp),%eax
177 8048165: 0f b6 00 movzbl (%eax),%eax
178 8048168: 84 c0 test %al,%al
179 804816a: 74 1a je 8048186 <strcmp+0x36>
181 804816c: 8b 45 0c mov 0xc(%ebp),%eax
182 804816f: 0f b6 00 movzbl (%eax),%eax
183 8048172: 84 c0 test %al,%al
184 8048174: 74 10 je 8048186 <strcmp+0x36>
186 8048176: 8b 45 08 mov 0x8(%ebp),%eax
187 8048179: 0f b6 10 movzbl (%eax),%edx
188 804817c: 8b 45 0c mov 0xc(%ebp),%eax
189 804817f: 0f b6 00 movzbl (%eax),%eax
190 8048182: 38 c2 cmp %al,%dl
191 8048184: 74 cf je 8048155 <strcmp+0x5>
194 8048186: 8b 45 08 mov 0x8(%ebp),%eax
195 8048189: 0f b6 10 movzbl (%eax),%edx
196 804818c: 8b 45 0c mov 0xc(%ebp),%eax
197 804818f: 0f b6 00 movzbl (%eax),%eax
198 8048192: 38 c2 cmp %al,%dl
199 8048194: 0f 94 c0 sete %al
200 8048197: 0f b6 c0 movzbl %al,%eax