Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structured Program Modular structure Program grouped into blocks, each performing one task Clear interface between blocks, Easy to write, read, debug and.

Similar presentations


Presentation on theme: "Structured Program Modular structure Program grouped into blocks, each performing one task Clear interface between blocks, Easy to write, read, debug and."— Presentation transcript:

1 Structured Program Modular structure Program grouped into blocks, each performing one task Clear interface between blocks, Easy to write, read, debug and maintain Top-down design Write outline, then refine Start from basic case, then add

2 Mathematics Input (–1) S B e–X 0.M Conversion to IEEE format Sign: OK Base: B e–X = 2 (log 2 B)(e–X) E.g. 4 10 =2 2*10, 8 10 =2 3*10, 16 10 =2 4*10 Mantissa: 0.M = 2 –i 1.m

3 Main Structure of Lab3 Read input B, E, X Read H and convert (code given) Extract exp from H, convert to IEEE Extract M from H, convert to IEEE Extract S from H Put everything together Print loop

4 Read Input B, E and X... msg1:.asciiz "Base (2, 4, 8 or 16): " msg2:.asciiz "Number of bits for exponent (5 to 10): " msg3:.asciiz "Excess of exponent: “... main:la $a0, msg1# print msg1 li $v0, 4 syscall li $v0, 5# read base syscall or $s1, $v0, $zero# $s1 stores B (base)... or $s2, $v0, $zero# $s2 stores E (no bits for e)... or $s3, $v0, $zero# $s3 stores X (excess)

5 Main Structure Read input B, E, X Read H and convert (code given) Extract exp from H, convert to IEEE Extract M from H, convert to IEEE Extract S from H Put everything together Print loop

6 Read H and Convert (given) loopjal read# read hex word into $s0... j loop read:# read 8 hex and store binary in $s0 li $t0, 0x00000030# t0 stores '0' li $t1, 0x00000061# t1 stores 'a‘... rend:jr $ra

7 Main Structure Read input B, E, X Read H and convert (code given) Extract exp from H, convert to IEEE Extract M from H, convert to IEEE Extract S from H Put everything together Print loop

8 Extract exp and Convert # H is in $s0 # assume E = 5 for now, extend later sll $t0, $s0, 1# get rid of sign srl $t0, $t0, 27# extract exp, 27=32-5 sub $t0, $t0, $s3# $t0 = exp–X # convert from base B to base 2 or $t1, $zero, $zero# $t1 = 0 bloop:srl $s1, $s1, 1# divide base by 2 beq $s1, $zero, bend add $t1, $t1, $t0 j bloop bend:addi $s1, $t1, 127# $s1 in excess 127 code

9 Main Structure Read input B, E, X Read H and convert (code given) Extract exp from H, convert to IEEE Extract M from H, convert to IEEE Extract S from H Put everything together Print loop

10 Extract M and Convert # H is in $s0, exp is in $s1 # assume E = 5 for now, extend later # extract mantissa in $t0 sll $t0, $s0, 6# get rid of exp, 6=5+1 srl $t0, $t0, 9# extracted mantissa # normalize li $t1, 0x00800000# $t1 = mask for hidden bit norm:and $t2, $t0, $t1 bne $t2, $zero, sign sll $t0, $t0, 1# shift left 1 bit addi $s1, $s1, -1# subtract exp by 1 j norm normd:li $t1, 0x007fffff and $t0, $t0, $t1# get rid of hidden bit

11 Main Structure Read input B, E, X Read H and convert (code given) Extract exp from H, convert to IEEE Extract M from H, convert to IEEE Extract S from H Put everything together Print loop

12 The Rest # H is in $s0, exp is in $s1, mantissa is in $t0 sign:li $t1, 0x80000000# $t1 = mask for sign and $t1, $s0, $t1# $t1 = sign # merge to for IEEE format sll $s0, $s1, 23# position exponent or $s0, $s0, $t1# merge sign and exponent or $s0, $s0, $t0# merge mantissa # print floating point sw $s0, 12($a1) lwc1 $f12, 12($a1) li $v0, 2# print single precision syscall

13 Extend to Arbitrary E # extract mantissa in $t0 srl $t0, $t0, 27# extract exp, 27=32-5... # extract mantissa in $t1 sll $t0, $s0, 6# get rid of exp, 6=5+1 addi $t1, $zero, 32 sub $t1, $t1, $s2# $s2 contains E jal shift... addi $t1, $s2, 1 sub $t1, $zero, $t1 jal shift

14 Shift Subroutine shift:# shift $t0 to right by $t1 bits slt $t2, $zero, $t1 beq $t2, $zero, left right:beq $t1, $zero, finish srl $t0, $t0, 1 addi $t1, $t1, -1 j right left:beq $t1, $zero, finish sll $t0, $t0, 1 addi $t1, $t1, 1 j left finish:jr $ra

15 High Level Language C++ void main() { int b, e, x; unsigned int h; char buf[100]; cout << “Input Base :”; cin >> b; cout << “Input No of bits for exp:”; cin >> e; cout << “Input excess:”; cin >> x; cout << “Input hexdecimal:” h = Char2Hex(buf); cout << Value(h, b, e, x); }

16 Convert String to HEX unsigned int Char2Hex(char a[]) { int i; unsigned int c, result; for (i = 0; i < 8; i ++) { if (isalpha(a[i])) c = a[i]-’a’+10; else c = a[i]-’0’; result = (result << 4) | c; } return result; }

17 Compute Value float value( unsigned int s0, // hex int s1,// base int s2,// no of bits for exp int s3)// excess { unsigned int m, e; m = ((s0 > 2; e = ((s0 > (32-s2)) – s3; return m*power(2, -30)*power(s1, e); }


Download ppt "Structured Program Modular structure Program grouped into blocks, each performing one task Clear interface between blocks, Easy to write, read, debug and."

Similar presentations


Ads by Google