Add TP01
This commit is contained in:
parent
7cc269efa4
commit
1f64bd761c
26
TP01/python/objects.py
Normal file
26
TP01/python/objects.py
Normal file
@ -0,0 +1,26 @@
|
||||
# Class without base class
|
||||
class Base():
|
||||
# Constructor (called by "Base()" when creating an object)
|
||||
def __init__(self):
|
||||
self.a = "set in Base's constructor"
|
||||
|
||||
def f(self):
|
||||
return self.a
|
||||
|
||||
def g(self):
|
||||
return "returned from Base's g()"
|
||||
|
||||
|
||||
class Derived(Base):
|
||||
# Function with the same name override the base class function.
|
||||
def f(self):
|
||||
return "returned from Derived's f()"
|
||||
|
||||
|
||||
b = Base() # Create an object of Base
|
||||
print(b.f()) # Method call, as usual OO languages
|
||||
print(b.g())
|
||||
|
||||
d = Derived()
|
||||
print(d.f())
|
||||
print(d.g())
|
53
TP01/python/type_unions.py
Normal file
53
TP01/python/type_unions.py
Normal file
@ -0,0 +1,53 @@
|
||||
from typing import List
|
||||
|
||||
# int | float means ``either an int or a float''.
|
||||
NUMBER = int | float # or Union[int, float] with Union imported from typing
|
||||
|
||||
|
||||
def add_numbers(a: NUMBER, b: NUMBER) -> NUMBER:
|
||||
return a + b
|
||||
|
||||
|
||||
# Both int and floats can be passed to the function
|
||||
print(add_numbers(1, 4.3))
|
||||
|
||||
|
||||
def divide_numbers(a: NUMBER, b: NUMBER) -> float:
|
||||
return a / b
|
||||
|
||||
|
||||
print(divide_numbers(1, 2))
|
||||
|
||||
# Declare the type of a list whose elements are numbers.
|
||||
LIST_OF_NUMBERS = List[NUMBER]
|
||||
|
||||
|
||||
def increment(a: LIST_OF_NUMBERS) -> LIST_OF_NUMBERS:
|
||||
return [x + 1 for x in a]
|
||||
|
||||
|
||||
print(increment([1, 2, 3]))
|
||||
|
||||
# Skip the end if you are late.
|
||||
|
||||
# The type DEEP_LIST_OF_NUMBERS is a special case since it references itself.
|
||||
# The identifier DEEP_LIST_OF_NUMBERS cannot be used before the end of its
|
||||
# initialization, but the circular dependency can be broken using the string
|
||||
# 'DEEP_LIST_OF_NUMBERS' instead.
|
||||
DEEP_LIST_OF_NUMBERS = NUMBER | List['DEEP_LIST_OF_NUMBERS']
|
||||
|
||||
|
||||
def deep_increment(d: DEEP_LIST_OF_NUMBERS) -> DEEP_LIST_OF_NUMBERS:
|
||||
if isinstance(d, list):
|
||||
# Note the unusual typing rule applied by Pyright here: because we are
|
||||
# in the 'isinstance(d, list)' branch, it knows that d is a list,
|
||||
# and accepts to iterate over it.
|
||||
return [deep_increment(e) for e in d]
|
||||
else:
|
||||
# ... and here, in the 'else' branch Pyright knows that d is
|
||||
# not a list,
|
||||
# and can deduce that it is a NUMBER.
|
||||
return d + 1
|
||||
|
||||
|
||||
print(deep_increment([1, [2, 3]]))
|
16
TP01/python/typecheck.py
Normal file
16
TP01/python/typecheck.py
Normal file
@ -0,0 +1,16 @@
|
||||
# Typing annotations for variables:
|
||||
# name: type
|
||||
int_variable: int
|
||||
float_variable: float
|
||||
int_variable = 4.2 # Static typing error, but no runtime error
|
||||
float_variable = 42.0 # OK
|
||||
float_variable = int_variable # OK
|
||||
|
||||
|
||||
# Typing annotations for functions (-> means "returns")
|
||||
def int_to_string(i: int) -> str:
|
||||
return str(i)
|
||||
|
||||
|
||||
print(int_to_string('Hello')) # Static typing error, but no runtime error
|
||||
print(int_to_string(42) / 5) # Both static and runtime error
|
6
TP01/riscv/asshand.s
Normal file
6
TP01/riscv/asshand.s
Normal file
@ -0,0 +1,6 @@
|
||||
.globl main
|
||||
main:
|
||||
addi a0, a0, 1
|
||||
bne a0, a0, main
|
||||
end:
|
||||
ret
|
14
TP01/riscv/bitcount.s
Normal file
14
TP01/riscv/bitcount.s
Normal file
@ -0,0 +1,14 @@
|
||||
.globl main
|
||||
main:
|
||||
addi sp,sp,-16
|
||||
sd ra,8(sp)
|
||||
## TODO Your assembly code there
|
||||
## END TODO End of user assembly code
|
||||
ld ra,8(sp)
|
||||
addi sp,sp,16
|
||||
ret
|
||||
|
||||
# Data comes here
|
||||
.section .data
|
||||
mydata:
|
||||
.dword 7
|
15
TP01/riscv/carres.s
Normal file
15
TP01/riscv/carres.s
Normal file
@ -0,0 +1,15 @@
|
||||
.text
|
||||
.globl main
|
||||
main:
|
||||
addi sp,sp,-16
|
||||
sd ra,8(sp)
|
||||
## TODO Your assembly code there
|
||||
## END TODO End of user assembly code
|
||||
ld ra,8(sp)
|
||||
addi sp,sp,16
|
||||
ret
|
||||
|
||||
# Data comes here
|
||||
.section .data
|
||||
mydata:
|
||||
.dword 7
|
12
TP01/riscv/ex1.c
Normal file
12
TP01/riscv/ex1.c
Normal file
@ -0,0 +1,12 @@
|
||||
/*Source code from Laure Gonnord, CAP Lab 1*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(){
|
||||
|
||||
int x = 41;
|
||||
x = x+1;
|
||||
printf("%d\n",x);
|
||||
|
||||
return 0;
|
||||
}
|
251
TP01/riscv/libprint.s
Normal file
251
TP01/riscv/libprint.s
Normal file
@ -0,0 +1,251 @@
|
||||
.globl println_int
|
||||
println_int:
|
||||
.globl println_bool
|
||||
println_bool:
|
||||
addi sp,sp,-8
|
||||
sd ra, 0(sp)
|
||||
call print_int
|
||||
call newline
|
||||
ld ra, 0(sp)
|
||||
addi sp,sp,8
|
||||
ret
|
||||
|
||||
.globl println_char
|
||||
println_char:
|
||||
addi sp,sp,-8
|
||||
sd ra, 0(sp)
|
||||
call print_char
|
||||
call newline
|
||||
ld ra, 0(sp)
|
||||
addi sp,sp,8
|
||||
ret
|
||||
|
||||
.text
|
||||
.align 1
|
||||
.globl println_string
|
||||
.type println_string, @function
|
||||
println_string: #address stored in a0
|
||||
addi sp,sp,-152
|
||||
sd ra,24(sp)
|
||||
sd s0,16(sp)
|
||||
addi s0,sp,32
|
||||
sd t0,32(sp)
|
||||
sd t1,40(sp)
|
||||
sd t2,48(sp)
|
||||
sd t3,56(sp)
|
||||
sd t4,64(sp)
|
||||
sd t5,72(sp)
|
||||
sd t6,80(sp)
|
||||
|
||||
sd a0,88(sp)
|
||||
sd a1,96(sp)
|
||||
sd a2,104(sp)
|
||||
sd a3,112(sp)
|
||||
sd a4,120(sp)
|
||||
sd a5,128(sp)
|
||||
sd a6,136(sp)
|
||||
sd a7,144(sp)
|
||||
|
||||
## Argument is already in a0, just forward it to puts
|
||||
call puts
|
||||
|
||||
ld ra,24(sp)
|
||||
ld s0,16(sp)
|
||||
|
||||
ld t0,32(sp)
|
||||
ld t1,40(sp)
|
||||
ld t2,48(sp)
|
||||
ld t3,56(sp)
|
||||
ld t4,64(sp)
|
||||
ld t5,72(sp)
|
||||
ld t6,80(sp)
|
||||
|
||||
ld a0,88(sp)
|
||||
ld a1,96(sp)
|
||||
ld a2,104(sp)
|
||||
ld a3,112(sp)
|
||||
ld a4,120(sp)
|
||||
ld a5,128(sp)
|
||||
ld a6,136(sp)
|
||||
ld a7,144(sp)
|
||||
|
||||
addi sp,sp,152
|
||||
jr ra
|
||||
.size println_string, .-println_string
|
||||
.section .rodata
|
||||
.align 3
|
||||
fmt_int:
|
||||
.string "%ld"
|
||||
str_empty:
|
||||
.string ""
|
||||
.text
|
||||
.align 1
|
||||
.globl print_int
|
||||
.type print_int, @function
|
||||
print_int: # print int stored in a0, saves/restores all scratch registers (except ft<n> which we don't use)
|
||||
addi sp,sp,-152
|
||||
|
||||
sd ra,24(sp)
|
||||
sd s0,16(sp)
|
||||
|
||||
sd t0,32(sp)
|
||||
sd t1,40(sp)
|
||||
sd t2,48(sp)
|
||||
sd t3,56(sp)
|
||||
sd t4,64(sp)
|
||||
sd t5,72(sp)
|
||||
sd t6,80(sp)
|
||||
|
||||
sd a0,88(sp)
|
||||
sd a1,96(sp)
|
||||
sd a2,104(sp)
|
||||
sd a3,112(sp)
|
||||
sd a4,120(sp)
|
||||
sd a5,128(sp)
|
||||
sd a6,136(sp)
|
||||
sd a7,144(sp)
|
||||
|
||||
## first parameter of print_int is second parameter of printf
|
||||
mv a1,a0
|
||||
## first parameter of printf is the format string
|
||||
la a0,fmt_int
|
||||
call printf
|
||||
|
||||
ld ra,24(sp)
|
||||
ld s0,16(sp)
|
||||
|
||||
ld t0,32(sp)
|
||||
ld t1,40(sp)
|
||||
ld t2,48(sp)
|
||||
ld t3,56(sp)
|
||||
ld t4,64(sp)
|
||||
ld t5,72(sp)
|
||||
ld t6,80(sp)
|
||||
|
||||
ld a0,88(sp)
|
||||
ld a1,96(sp)
|
||||
ld a2,104(sp)
|
||||
ld a3,112(sp)
|
||||
ld a4,120(sp)
|
||||
ld a5,128(sp)
|
||||
ld a6,136(sp)
|
||||
ld a7,144(sp)
|
||||
|
||||
addi sp,sp,152
|
||||
jr ra
|
||||
.size print_int, .-print_int
|
||||
.align 1
|
||||
.globl newline
|
||||
.type newline, @function
|
||||
newline: # print int stored in a0, saves/restores all scratch registers (except ft<n> which we don't use)
|
||||
addi sp,sp,-152
|
||||
|
||||
sd ra,24(sp)
|
||||
sd s0,16(sp)
|
||||
|
||||
sd t0,32(sp)
|
||||
sd t1,40(sp)
|
||||
sd t2,48(sp)
|
||||
sd t3,56(sp)
|
||||
sd t4,64(sp)
|
||||
sd t5,72(sp)
|
||||
sd t6,80(sp)
|
||||
|
||||
sd a0,88(sp)
|
||||
sd a1,96(sp)
|
||||
sd a2,104(sp)
|
||||
sd a3,112(sp)
|
||||
sd a4,120(sp)
|
||||
sd a5,128(sp)
|
||||
sd a6,136(sp)
|
||||
sd a7,144(sp)
|
||||
|
||||
## first parameter of printf is the format string
|
||||
la a0,str_empty
|
||||
call puts
|
||||
|
||||
ld ra,24(sp)
|
||||
ld s0,16(sp)
|
||||
|
||||
ld t0,32(sp)
|
||||
ld t1,40(sp)
|
||||
ld t2,48(sp)
|
||||
ld t3,56(sp)
|
||||
ld t4,64(sp)
|
||||
ld t5,72(sp)
|
||||
ld t6,80(sp)
|
||||
|
||||
ld a0,88(sp)
|
||||
ld a1,96(sp)
|
||||
ld a2,104(sp)
|
||||
ld a3,112(sp)
|
||||
ld a4,120(sp)
|
||||
ld a5,128(sp)
|
||||
ld a6,136(sp)
|
||||
ld a7,144(sp)
|
||||
|
||||
addi sp,sp,152
|
||||
jr ra
|
||||
.size newline, .-newline
|
||||
.align 1
|
||||
.globl print_char
|
||||
.type print_char, @function
|
||||
print_char: # print char stored in a0 (ascii code)
|
||||
addi sp,sp,-152
|
||||
sd ra,24(sp)
|
||||
sd s0,16(sp)
|
||||
|
||||
addi s0,sp,32
|
||||
|
||||
|
||||
sd t0,32(sp)
|
||||
sd t1,40(sp)
|
||||
sd t2,48(sp)
|
||||
sd t3,56(sp)
|
||||
sd t4,64(sp)
|
||||
sd t5,72(sp)
|
||||
sd t6,80(sp)
|
||||
|
||||
sd a0,88(sp)
|
||||
sd a1,96(sp)
|
||||
sd a2,104(sp)
|
||||
sd a3,112(sp)
|
||||
sd a4,120(sp)
|
||||
sd a5,128(sp)
|
||||
sd a6,136(sp)
|
||||
sd a7,144(sp)
|
||||
|
||||
# call to putchar
|
||||
mv a5,a0
|
||||
sb a5,-17(s0)
|
||||
lbu a5,-17(s0)
|
||||
sext.w a5,a5
|
||||
mv a0,a5
|
||||
call putchar
|
||||
|
||||
#restore registers
|
||||
ld ra,24(sp)
|
||||
ld s0,16(sp)
|
||||
|
||||
ld t0,32(sp)
|
||||
ld t1,40(sp)
|
||||
ld t2,48(sp)
|
||||
ld t3,56(sp)
|
||||
ld t4,64(sp)
|
||||
ld t5,72(sp)
|
||||
ld t6,80(sp)
|
||||
|
||||
ld a0,88(sp)
|
||||
ld a1,96(sp)
|
||||
ld a2,104(sp)
|
||||
ld a3,112(sp)
|
||||
ld a4,120(sp)
|
||||
ld a5,128(sp)
|
||||
ld a6,136(sp)
|
||||
ld a7,144(sp)
|
||||
|
||||
addi sp,sp,152
|
||||
jr ra
|
||||
.size print_char, .-print_char
|
||||
|
||||
|
18
TP01/riscv/minmax.s
Normal file
18
TP01/riscv/minmax.s
Normal file
@ -0,0 +1,18 @@
|
||||
.text
|
||||
.globl main
|
||||
main:
|
||||
addi sp,sp,-16
|
||||
sd ra,8(sp)
|
||||
## TODO Your assembly code there
|
||||
## END TODO End of user assembly code
|
||||
ld ra,8(sp)
|
||||
addi sp,sp,16
|
||||
ret
|
||||
|
||||
# Data comes here
|
||||
.section .data
|
||||
mydata:
|
||||
.dword 7
|
||||
.dword 42
|
||||
min:
|
||||
.dword 0
|
26
TP01/riscv/test_print.s
Normal file
26
TP01/riscv/test_print.s
Normal file
@ -0,0 +1,26 @@
|
||||
.section .text
|
||||
.globl main
|
||||
main:
|
||||
addi sp,sp,-16
|
||||
sd ra,8(sp)
|
||||
## Your assembly code there
|
||||
la a0, .LC1
|
||||
call println_string
|
||||
li a0,42
|
||||
call print_int
|
||||
call newline
|
||||
li a0,97
|
||||
call print_char
|
||||
li a0,10 #new line char
|
||||
call print_char
|
||||
|
||||
## /end of user assembly code
|
||||
ld ra,8(sp)
|
||||
addi sp,sp,16
|
||||
ret
|
||||
|
||||
# Data comes here
|
||||
.section .data
|
||||
.align 3
|
||||
.LC1:
|
||||
.string "HI MIF08!"
|
15
TP01/riscv/triangles.s
Normal file
15
TP01/riscv/triangles.s
Normal file
@ -0,0 +1,15 @@
|
||||
.text
|
||||
.globl main
|
||||
main:
|
||||
addi sp,sp,-16
|
||||
sd ra,8(sp)
|
||||
## TODO Your assembly code there
|
||||
## END TODO End of user assembly code
|
||||
ld ra,8(sp)
|
||||
addi sp,sp,16
|
||||
ret
|
||||
|
||||
# Data comes here
|
||||
.section .data
|
||||
mydata:
|
||||
.dword 7
|
BIN
TP01/tp1.pdf
Normal file
BIN
TP01/tp1.pdf
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user