4.50

/* switch.ys */
.pos 0
	irmovq stack, %rsp
	call main
	halt

# Array of 4 elements
.align 8
array:
  .quad 0x0000000000000000
  .quad 0x0000000000000000
  .quad 0x0000000000000000
  .quad 0x0000000000000000

main:
  # test number 1, -1, 3, 5
  irmovq array, %r10

  irmovq $1,%rdi
	call switchv
  rmmovq %rax, (%r10)

  irmovq $-1,%rdi
	call switchv
  rmmovq %rax, 8(%r10)

  irmovq $3,%rdi
	call switchv
  rmmovq %rax, 16(%r10)

  irmovq $5,%rdi
	call switchv
  rmmovq %rax, 24(%r10)
	ret

table:
  .quad LD  # default branch
  .quad L0  # idx == 0
  .quad L1  # idx == 1
  .quad L2  # idx == 2
  .quad L3  # idx == 3
  .quad L4  # idx == 4
  .quad L5  # idx == 5

# long switchv(long idx)
# idx in %rdi
switchv:
  # contant number
  irmovq $8, %r8
  irmovq $0, %r10
  irmovq $1, %r11

  irmovq $0, %rax
  irmovq table, %rcx # table address
  rrmovq %rdi, %rdx
  subq %r8, %rdx
  jg def 			 # idx > 5
  subq %r10, %rdi
  jl def 			 # idx < 0
mul: # calculate 8 * %rdi
  subq %r10, %rdi
  je addr
  addq %r8, %rcx
  subq %r11, %rdi
  jmp mul
addr: # jump using table address
  addq %r8, %rcx
  mrmovq (%rcx), %rdi
  pushq %rdi
  ret
def: # default branch
  irmovq table, %rcx
  mrmovq (%rcx), %rdi
  pushq %rdi
  ret
L0:
  irmovq $0xaaa, %rax
  ret
L1:
  jmp LD
L2:
  jmp L5
L3:
  irmovq $0xccc, %rax
  ret
L4:
  jmp LD
L5:
  irmovq $0xbbb, %rax
  ret
LD:
  irmovq $0xddd, %rax
  ret

.pos 0x200
stack:

test function switchv in main, using idx 1,-1,3,5, store result into array

test and output, watch changes to memory:

../sim/misc/yas switch.ys
../sim/misc/yis switch.yo
Stopped in 133 steps at PC = 0x13.  Status 'HLT', CC Z=0 S=0 O=0
Changes to registers:
%rax:	0x0000000000000000	0x0000000000000bbb
%rcx:	0x0000000000000000	0x00000000000000e7
%rdx:	0x0000000000000000	0xfffffffffffffffd
%rsp:	0x0000000000000000	0x0000000000000200
%rdi:	0x0000000000000000	0x00000000000001a8
%r8:	0x0000000000000000	0x0000000000000008
%r11:	0x0000000000000000	0x0000000000000001

Changes to memory:
0x0000:	0x000000000200f430	0x0000000000000ddd
0x0008:	0x0000000038800000	0x0000000000000ddd
0x0010:	0x0000000000000000	0x0000000000000ccc
0x0018:	0x0000000000000000	0x0000000000000bbb
comments powered by Disqus