Structured Program - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Structured Program

Description:

Structured Program – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 18
Provided by: Weipi2
Category:

less

Transcript and Presenter's Notes

Title: Structured Program


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 BeX 0.M
  • Conversion to IEEE format
  • Sign OK
  • Base BeX 2 (log2B)(eX)
  • E.g. 4102210, 8102310, 16102410
  • Mantissa 0.M 2i 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)
loop jal 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, 2732-5 sub t0,
t0, s3 t0 expX 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, 651 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, 2732-5 ... extract mantissa in
t1 sll t0, s0, 6 get rid of exp, 651
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 buf100 cout ltlt Input Base
cin gtgt b cout ltlt Input No of bits for
exp cin gtgt e cout ltlt Input excess cin
gtgt x cout ltlt Input hexdecimal h
Char2Hex(buf) cout ltlt 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 lt 8
i ) if (isalpha(ai)) c
ai-a10 else c ai-0 result
(result ltlt 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
ltlt (1s2)) gtgt 2 e ((s0 ltlt 1) gtgt (32-s2))
s3 return mpower(2, -30)power(s1, e)
Write a Comment
User Comments (0)
About PowerShow.com