LAB4 final

Zahra · January 14, 2021 · Assembly
%macro rw 4
    mov rax, %1
    mov rdi, %2
    mov rsi, %3
    mov rdx, %4
    syscall
%endmacro

section .data
    menu db 13d, "----Menu----"
         db 10d, "1.Enter a string"
         db 10d, "2.Find length of string"
         db 10d, "3.Reverse entered string"
         db 10d, "4.Palindrome check for string"
         db 10d, "5.Exit program"
         
    menulen equ $-menu
    newline db 0ah
    
    msg1 db "1.Enter string: ", 
    l1 equ $-msg1
    msg2 db "2.Length of string is: "
    l2 equ $-msg2
    msg3 db "  Reversed string is: "
    l3 equ $-msg3
    msg5 db "3.Original string is: "
    l5 equ $-msg5
    yes db "4.String is a palindrome!"
    l6 equ $-yes
    no db "4.String is not a palindrome!"
    l7 equ $-no
    quit db "5.Exit code"
    l8 equ $-quit

section .bss
    string resb 15
    length resb 4
    result resb 4
    reverse resb 15
    
section .text
    global _start

_start:
    rw 1,1,menu,menulen ;displaying the operations in order of execution
    rw 1,1,newline,1
    rw 1,1,newline,1
    rw 1,1,msg1,l1
    rw 0,0,string,15
    
    CALL display_length     
    
    CALL display_reverse   
    
    
    rw 1,1,msg5,l5          ;Calling macro to display messages
    rw 1,1,string,15     
    rw 1,1,newline,1
    rw 1,1,msg3,l3
    rw 1,1,reverse,15
    rw 1,1,newline,1
    
    CALL display_palindrome
    
    rw 1,1,quit,l8
    rw 60,0,0,0             
    
display_length:
    
    MOV byte[length], al ;Moving length of string from AL into length variable
    
    
    rw 1,1,string,15      ;Calling macro to display messages
    rw 1,1,newline,1
    rw 1,1,msg2,l2
    
    MOV al, byte[length]
    CMP al, 09
    
    JBE down1    ;If number is greater than 9, add 7H (9 is 39H and A is 41H)        
    ADD al, 07h
down1:
    
    ADD al, 30h    ;if number is less than 9, add 30H to convert to ASCII
    MOV byte[result], al
    
    
    rw 1,1,result,1 ;printing length in ASCII format to user
    rw 1,1,newline,1
    RET
    
display_reverse:
    
    MOV rsi, string  ;Initializing the pointers
    MOV rdi, reverse

    MOV rcx, 00h         ;Initializing counter to length of input string
    MOV cl, byte[length] 
    
    
    ADD di,cx         ;Set destination ptr to end of reverse       
    DEC rdi
up1:
                                     ;Moving character by character into al register
    MOV al, byte[rsi]  
                                  ;Copy first letter of string into last place of reverse using AL register
    MOV byte[rdi], al 
                                 ;Incrementing string ptr 
    INC rsi
                              ;Decrement reverse pointer and counter register
    DEC rdi
    DEC cx
              ;Copy characters till cx is not zero
    JNZ up1
    RET
    
display_palindrome:
    
    MOV	rsi, string  ;Initialize pointer to start of string
    
    MOV rdi, reverse    ;Initialize pointer to start of reverse
    
    MOV cl, byte[length]   ;Initialize counter to length of string
up2: 
    MOV al, byte[rsi]

    CMP al, byte[rdi]      ;Compare character in string to character in reverse
    
    JNZ down2  ;If characters not equal, exit loop
    
    ;Incrementing rsi and rdi registers: String operations
    inc rsi
    inc rdi
    dec cx
	JNZ up2
	
	;printing message is a palindrome to user
	rw 1,1,yes,l6        
	rw 1,1,newline,1
	RET
down2:
    ;print not a palindrome message to user
    rw 1,1,no,l7
    rw 1,1,newline,1    
    RET

Comments

Please sign up or log in to contribute to the discussion.