Global Variables Global variables Variables that declared outside
Global Variables § Global variables Variables that declared outside any functions but are accessible by all functions C: variables usually declared at the beginning of files • Static variables Java: class instance variables, static variables § Use of global variable Modern programming recommends against excessive use of global variables It is useful, however, in practice 10/23/2021 CMPUT 229 1
Example of using global variables § A user-created memory management package create_list_buff() relase_list_buff() get_one_buff() § Buff is a memory unit with some access methods, such as buff->get_size(buff) buff->append(buff) § Sample program (buff. c buff. h) 10/23/2021 CMPUT 229 2
How to allocate global variables in MIPS ? § § Registers ? Data Segments ? Stack ? Global area ? Global pointer pointing to the middle of a 64 K block of memory § SDA (small data area) 10/23/2021 CMPUT 229 3
Use of global register § The specified global area Just use the initial value of $gp § Stack Move $sp, $gp § Data area Allocate the memory la $gp, globa 10/23/2021 CMPUT 229 4
Example. data gdata: . word 1, 2, 4 10/23/2021 . text main: la lw lw CMPUT 229 $gp, gdata $t 1, ($gp) $t 2, 4($gp) 5
Example sub $sp, 48 Move $gp, $sp lw $t 2, 0($gp) lb $t 3, 4($gp) 10/23/2021 CMPUT 229 6
Efficiency between data area and global area § To load the word in the data segment at address 10010020 into register $vo lui $s 0, 0 x 1001 # 0 x 1001 means 1001 base 16 lw $v 0, 0 x 0020($s 0) # 0 x 10010000 + 0 x 0020 = 0 x 10010020 § To load the word using $gp (10008000 ) lw $vo, 0 x 8020 10/23/2021 CMPUT 229 7
- Slides: 7