This project was done for the ‘Computer Organization and Assembly Language’ class, with Prof. Borin from Unicamp.
The Hamming Code (also called 7-4 code) is an error-correcting code used to make transmission and store of data less error-prone. It adds 3 parity bits to every 4 bits (hence 7-4). You can read the details here. My ARM assembly implementation follows:
.global main
.extern scanf
.extern printf
.data
ipat: .asciz "%x"
opat: .asciz "%xn"
opat2: .asciz "%xn%xn"
numero: .word 0
.text
.align 4
@definicao da funcao main
main:
push {ip, lr}
@ testa parte de codificacao
@ pede ao usuario para informar um valor
ldr r0, =ipat
ldr r1, =numero
bl scanf
@ codifica o valor informado
ldr r0, =numero
ldr r0, [r0]
bl codifica
@ retorna ao usuario o valor codificado
mov r1, r0
ldr r0, =opat
bl printf
@ testa a parte de de-codificacao
@pede ao usuario para informar um valor
ldr r0, =ipat
ldr r1, =numero
bl scanf
@ decodifica o valor informado
ldr r0, =numero
ldr r0, [r0]
bl decodifica
@retorna ao usuario o valor decodificado e o status de erro
mov r2, r1
mov r1, r0
ldr r0, =opat2
bl printf
mov r0, #0
pop {ip, pc}
@ definicao da funcao r0=codifica(r0)
codifica:
push {r4-r10}
@ isolate bits r1 = d1 r2 = d2 r3 = d3 r4 = d4
mov r5, r0
mov r4, #1
and r4, r4, r5
lsr r5, r5, #1
mov r3, #1
and r3, r3, r5
lsr r5, r5, #1
mov r2, #1
and r2, r2, r5
lsr r5, r5, #1
mov r1, #1
and r1, r1, r5
@ calculate p1, put in r5
eor r5, r4, r2
eor r5, r5, r1
@calculate p2, put in r6
eor r6, r4, r3
eor r6, r6, r1
@calculate p3, put in r7
eor r7, r4, r3
eor r7, r7, r2
@encode output
mov r0, #0
orr r0, r0, r4
lsl r3, r3, #1
orr r0, r0, r3
lsl r2, r2, #2
orr r0, r0, r2
lsl r7, r7, #3
orr r0, r0, r7
lsl r1, r1, #4
orr r0, r0, r1
lsl r6, r6, #5
orr r0, r0, r6
lsl r5, r5, #6
orr r0, r0, r5
pop {r4-r10}
bx lr
@ definicao da funcao (r0, r1)=decodifica(r0)
decodifica:
push {r4-r10}
mov r8, r0
@ isolate bits r1=d1 r2=d2 r3=d3 r4=d4 r5=p1 r6=p2 r7=p3
mov r4, #1
and r4, r4, r0
lsr r0, r0, #1
mov r3, #1
and r3, r3, r0
lsr r0, r0, #1
mov r2, #1
and r2, r2, r0
lsr r0, r0, #1
mov r7, #1
and r7, r7, r0
lsr r0, r0, #1
mov r1, #1
and r1, r1, r0
lsr r0, r0, #1
mov r6, #1
and r6, r6, r0
lsr r0, r0, #1
mov r5, #1
and r5, r5, r0
@restore original bit pattern on r0
mov r0, #1
and r0, r0, r1
lsl r0, r0, #3
mov r9, #7
and r8, r8, r9
orr r0, r0, r8
@error flag=0
mov r10, #0
@validate p1
eor r9, r1, r2
eor r9, r9, r4
eor r9, r9, r5
cmp r9, #0
beq continuep1
mov r10, #1
continuep1:
@validate p2
eor r9, r1, r3
eor r9, r9, r4
eor r9, r9, r6
cmp r9, #0
beq continuep2
mov r10, #1
continuep2:
@validate p3
eor r9, r2, r3
eor r9, r9, r4
eor r9, r9, r7
cmp r9, #0
beq continuep3
mov r10, #1
continuep3:
mov r1, r10
pop {r4-r10}
bx lr