sliver__

2.9 Communicating with People 본문

CS/컴퓨터 구조

2.9 Communicating with People

sliver__ 2023. 10. 21. 11:33
728x90

ASCII code

Quiz) 아래 코드를 assembly language로 바꾸면?

void strcpy (char x[], char y[]) 
{
    int i; 
    i = 0;
    while ((x[i] = y[i]) != ‘\0’) /* copy & test byte */ 
    i += 1;
}

==>

strcpy:
	addi $sp, $sp, -4
    sw $s0, 0($sp)
    add $s0, $zero, $zero
L1:
    add $t1, $s0, $a1 # address of y[i] in $t1
    lbu $t2, 0($t1)
    add $t3, $s0, $a0
    sb $t2, 0($t3)
    beq $t2, $zero, L2 # y[i] == 0, go to L2
    addi $s0, $s0, 1
    j L1
L2:
    lw $s0, 0($sp) # Restore $s0 from memory
    addi $sp, $sp, 4 # Pop 1 word off stack
    jr $ra

 

Characters and Strings in Java

16 bit를 load / store 하는 명령어는 Load half(lh).

lh는 half word를 memory에서 가져오고 reigster의 오른쪽 16bit에 저장.

signed number인 경우, signed extends를 통해 왼쪽 16비트에 채움

 

unsigned 16bits 명령어를 lhu.

store half word(sh)는 register의 오른쪽 16비트를 memory에 저장

 

ex) lhu $t0, 0($sp)

      sh $t0, 0($gp)

 

!! C string의 경우 char이지만 align 을 위해 4bytes(word)를 점유한다.

728x90
Comments