So I wrote this assembly code to display the 2\'s compliment of an input number,
ID: 3665169 • Letter: S
Question
So I wrote this assembly code to display the 2's compliment of an input number, the input is incremented by 1 everytime I push a button (so in essence, I push the button it starts off at 1 displays 2's compliment, i push the button it goes to 2 and displays the 2's compliment and so on) but I want to stop at the 2's compliment of 7 and loop it back to zero to start over again but I can't seem to figure it out. The programs keeps counting all the way to 15 and then resets. I originally was trying to:
cmp.b 0x09,R5
bic.b 0x00,R5
jnz Skip
Skip
call #delay
But like i said the program keeps reading to 15. Any help on this would be greatly appreciated. Below is my code without the reset at 7.
;-------------------------------------------------------------------------------
; MSP430 Assembler Code Template for use with TI Code Composer Studio
;
;
;-------------------------------------------------------------------------------
.cdecls C,LIST,"msp430.h" ; Include device header file
;-------------------------------------------------------------------------------
.def RESET ; Export program entry-point to
; make it known to linker.
;-------------------------------------------------------------------------------
.text ; Assemble into program memory.
.retain ; Override ELF conditional linking
; and retain current section.
.retainrefs ; And retain any sections that have
; references to current section.
;-------------------------------------------------------------------------------
RESET mov.w #__STACK_END,SP ; Initialize stackpointer
StopWDT mov.w #WDTPW|WDTHOLD,&WDTCTL ; Stop watchdog timer
;-------------------------------------------------------------------------------
; MAIN LOOP HERE
;-------------------------------------------------------------------------------
MAIN
mov.w #0xFFFF,&P1DIR ;turn on all port 1
mov.w #0x00,&P2DIR ;turn on port 2
mov.w #0x01,R5 ;this is the amount that the counter is going to be incrementing by
mov.w #0x00,R8 ;start at zero, assigned to R8
LOOPY
cmp.w &P2IN,R5 ;Whenever BUTTON is pressed: port 2 compared to register 5
jnz LOOPY ;jump to LOOPY if not zero
add.w #0x01,R8 ;increments the value of register 8 by 1
mov.w R8,R12 ;storing the incremented numbers of R8 to R12
xor.w #0xFF,R12 ;XOR by 15 to the value of register 12
add.w #0x01,R12 ;adding one to the value of register 12
mov.w R12,&P1OUT ;displays the 2's compliment on LEDs
call #delay
jmp LOOPY
delay:
mov.w #00006h,R11
loop_a mov.w #0D901h,R10
loop_b dec.w R10
jnz loop_b
dec.w R11
jnz loop_a
ret
;-------------------------------------------------------------------------------
; Stack Pointer definition
;-------------------------------------------------------------------------------
.global __STACK_END
.sect .stack
;-------------------------------------------------------------------------------
; Interrupt Vectors
;-------------------------------------------------------------------------------
.sect ".reset" ; MSP430 RESET Vector
.short RESET
Explanation / Answer
.Ltext0:
.section .rodata
.LC0:
0000 54776F20 .string "Two s complement is %s"
7320636F
6D706C65
6D656E74
20697320
.text
.globl findTwosComplmnt
findTwosComplmnt:
.LFB0:
.cfi_startproc
0000 55 pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
0001 4889E5 movq %rsp, %rbp
.cfi_def_cfa_register 6
0004 4883EC40 subq $64, %rsp
0008 48897DC8 movq %rdi, -56(%rbp)
000c 64488B04 movq %fs:40, %rax
25280000
00
0015 488945F8 movq %rax, -8(%rbp)
0019 31C0 xorl %eax, %eax
001b C745D800 movl $0, -40(%rbp)
000000
0022 488B45C8 movq -56(%rbp), %rax
0026 4889C7 movq %rax, %rdi
0029 E8000000 call strlen
00
002e 8945DC movl %eax, -36(%rbp)
0031 8B45DC movl -36(%rbp), %eax
0034 83E801 subl $1, %eax
0037 8945D4 movl %eax, -44(%rbp)
003a EB2E jmp .L2
.L5:
003c 8B45D4 movl -44(%rbp), %eax
003f 4863D0 movslq %eax, %rdx
0042 488B45C8 movq -56(%rbp), %rax
0046 4801D0 addq %rdx, %rax
0049 0FB600 movzbl (%rax), %eax
004c 3C30 cmpb $48, %al
004e 750C jne .L3
0050 8B45D4 movl -44(%rbp), %eax
0053 4898 cltq
0055 C64405E0 movb $49, -32(%rbp,%rax)
31
005a EB0A jmp .L4
.L3:
005c 8B45D4 movl -44(%rbp), %eax
005f 4898 cltq
0061 C64405E0 movb $48, -32(%rbp,%rax)
30
.L4:
0066 836DD401 subl $1, -44(%rbp)
.L2:
006a 837DD400 cmpl $0, -44(%rbp)
006e 79CC jns .L5
0070 8B45DC movl -36(%rbp), %eax
0073 83E801 subl $1, %eax
0076 8945D4 movl %eax, -44(%rbp)
0079 E9880000 jmp .L6
00
.L12:
007e 8B45DC movl -36(%rbp), %eax
0081 83E801 subl $1, %eax
0084 3B45D4 cmpl -44(%rbp), %eax
0087 752D jne .L7
0089 8B45D4 movl -44(%rbp), %eax
008c 4898 cltq
008e 0FB64405 movzbl -32(%rbp,%rax), %eax
E0
0093 3C30 cmpb $48, %al
0095 750C jne .L8
0097 8B45D4 movl -44(%rbp), %eax
009a 4898 cltq
009c C64405E0 movb $49, -32(%rbp,%rax)
31
00a1 EB5F jmp .L10
.L8:
00a3 8B45D4 movl -44(%rbp), %eax
00a6 4898 cltq
00a8 C64405E0 movb $48, -32(%rbp,%rax)
30
00ad C745D801 movl $1, -40(%rbp)
000000
00b4 EB4C jmp .L10
.L7:
00b6 837DD801 cmpl $1, -40(%rbp)
00ba 7521 jne .L11
00bc 8B45D4 movl -44(%rbp), %eax
00bf 4898 cltq
00c1 0FB64405 movzbl -32(%rbp,%rax), %eax
E0
00c6 3C30 cmpb $48, %al
00c8 7513 jne .L11
00ca 8B45D4 movl -44(%rbp), %eax
00cd 4898 cltq
00cf C64405E0 movb $49, -32(%rbp,%rax)
31
00d4 C745D800 movl $0, -40(%rbp)
000000
00db EB25 jmp .L10
.L11:
00dd 837DD801 cmpl $1, -40(%rbp)
00e1 751F jne .L10
00e3 8B45D4 movl -44(%rbp), %eax
00e6 4898 cltq
00e8 0FB64405 movzbl -32(%rbp,%rax), %eax
E0
00ed 3C31 cmpb $49, %al
00ef 7511 jne .L10
00f1 8B45D4 movl -44(%rbp), %eax
00f4 4898 cltq
00f6 C64405E0 movb $48, -32(%rbp,%rax)
30
00fb C745D801 movl $1, -40(%rbp)
000000
.L10:
0102 836DD401 subl $1, -44(%rbp)
.L6:
0106 837DD400 cmpl $0, -44(%rbp)
010a 0F896EFF jns .L12
FFFF
0110 8B45DC movl -36(%rbp), %eax
0113 4898 cltq
0115 C64405E0 movb $0, -32(%rbp,%rax)
00
011a 488D45E0 leaq -32(%rbp), %rax
011e 4889C6 movq %rax, %rsi
0121 BF000000 movl $.LC0, %edi
00
0126 B8000000 movl $0, %eax
00
012b E8000000 call printf
00
0130 488B45F8 movq -8(%rbp), %rax
0134 64483304 xorq %fs:40, %rax
25280000
00
013d 7405 je .L13
013f E8000000 call __stack_chk_fail
00
.L13:
0144 C9 leave
.cfi_def_cfa 7, 8
0145 C3 ret
.cfi_endproc
.LFE0:
.section .rodata
0017 00 .align 8
.LC1:
0018 496E7075 .string "Input the binary number for which 2's compliment is to be found"
74207468
65206269
6E617279
206E756D
.LC2:
0058 257300 .string "%s"
.text
.globl main
main:
.LFB1:
.cfi_startproc
0146 55 pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
0147 4889E5 movq %rsp, %rbp
.cfi_def_cfa_register 6
014a 4883EC30 subq $48, %rsp
014e 64488B04 movq %fs:40, %rax
25280000
00
0157 488945F8 movq %rax, -8(%rbp)
015b 31C0 xorl %eax, %eax
.LBB2:
015d C745DC01 movl $1, -36(%rbp)
000000
0164 EB35 jmp .L15
.L16:
0197 8345DC01 addl $1, -36(%rbp)
0166 BF000000 movl $.LC1, %edi
00
016b B8000000 movl $0, %eax
00
0170 E8000000 call printf
00
0175 488D45E0 leaq -32(%rbp), %rax
0179 4889C6 movq %rax, %rsi
017c BF000000 movl $.LC2, %edi
00
0181 B8000000 movl $0, %eax
00
0186 E8000000 call __isoc99_scanf
00
018b 488D45E0 leaq -32(%rbp), %rax
018f 4889C7 movq %rax, %rdi
0192 E8000000 call findTwosComplmnt
00
.L15:
019b 837DDC07 cmpl $7, -36(%rbp)
019f 7EC5 jle .L16
.LBE2:
01a1 B8000000 movl $0, %eax
00
01a6 488B55F8 movq -8(%rbp), %rdx
01aa 64483314 xorq %fs:40, %rdx
25280000
00
01b3 7405 je .L18
01b5 E8000000 call __stack_chk_fail
00
.L18:
01ba C9 leave
.cfi_def_cfa 7, 8
01bb C3 ret
.cfi_endproc
.LFE1:
.Letext0:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.