Assembly Language Logical Instructions and destination source and
Assembly Language
Logical Instructions and destination, source and AL, 01 H or destination, source or AX, FAH xor destination, source xor EAX, DDFFH not destination not EBX
Logical Instructions and AL, 01 H je bit_is_zero <code to be executed when the bit is one> jmp skip 1 bit_is_zero: <code to be executed when the bit is zero> skip 1: <rest of the code>
Shift Instructions shl (Shif Left) and shr (Shift Right). These instruction is used to left/right-shift a 8, 16, or 32 bit destination operand stored either in a register or in memory. – shl (Shif Left) [shl destination, count] shifts to the left by one bit position causes the leftmost bit to move to the carry flag (CF). – shl (Shif Left) [shr destination, count] shifts to the right by one bit position causes the rightmost bit to move to the carry flag (CF).
Shift Instructions Examples
Shift Instructions Testing the least significant bit of the data in the AL register. . shr AL, 1 jnc bit_is_zero <Executed when the bit is one> jmp skip 1 bit_is_zero: <Executed when the bit is zero> skip 1: <rest of the code>
Rotate Instructions • Rotate without carry The rol instruction [rol destination, count] performs left rotation with the bits falling off on the left placed on the right side – The ror instruction [ror destination, count] performs right rotation
Rotate Instructions Examples
Rotate Instructions • Rotate Through Carry The rcl (Rotate through Carry Left) and rcr (Rotate through Carry Right) instruction include the carry flag in the rotation process. – rcl AL, 1 ; [rcl destination, count] – rcr BL, 1 ; [rcl destination, count]
Rotate Instructions Examples
Defining Constants The EQU Directive Format: name EQU expression Assigns the result of the expression to name. Example: NUM_OF_STUDENTS EQU 90 Assigns 90 to NUM_OF_STUDENTS … mov ECX, NUM_OF_STUDENTS cmp EAX, NUM_OF_STUDENTS Example: NUM_OF_ROWS EQU 50 NUM_OF_COLS EQU 10 ARRAY_SIZE EQU NUM_OF_ROWS * NUM_OF_COLS
Macros • A mean to represent a block (code or data) using a name, called macro name Format: %macro_name para_count <macro body> %endmacro Example: %macro double. AX shl AX, 1 %endmacro Using a Macro: mov AX, 23 double. AX
Macros Macro with Parameters Example: %macro mult_by_16 1 shl %1, 4 %endmacro Using a Macro: mov BX, 15 mult_by_16 BX
Illustration Example ; Convert an input the asky of an input key into binary. DATA prompt db "Please input a character: ", 0 ascii_msg db "The ASCII code of ’", 0 binary_msg db "’ in binary is ", 0 query_msg db "Do you want to quit (Y/N): ", 0. CODE. STARTUP read_char: Put. Str prompt ; request a char. input Get. Ch AL ; read input character Put. Str ascii_msg Put. Ch AL Put. Str binary_msg mov AH, 80 H ; mask byte = 80 H mov ECX, 8 ; loop count to print 8 bits
Illustration Example print_bit: test AL, AH ; test does not modify AL jz print_0 ; if tested bit is 0, print it Put. Ch ’ 1’ ; otherwise, print 1 jmp skip 1 print_0: Put. Ch ’ 0’ ; print 0 skip 1: shr AH, 1 loop print_bit newline Put. Str query_msg Get. Ch AL cmp AL, ’Y’ jne read_char done: . EXIT ; right-shift mask bit to test ; next bit of the ASCII code ; query user whether to terminate ; read response ; if response is not ’Y’ ; read another character ; terminate program
Illustration Example %include "io. mac". DATA char_prompt db "Please input a character: ", 0 out_msg 1 db "The ASCII code of ’", 0 out_msg 2 db "’ in hex is ", 0 query_msg db "Do you want to quit (Y/N): ", 0. CODE. STARTUP read_char: Put. Str char_prompt ; request a char. input Get. Ch AL ; read input character Put. Str out_msg 1 Put. Ch AL Put. Str out_msg 2 mov AH, AL ; save input character in AH shr AL, 4 ; move upper 4 bits to lower half mov CX, 2 ; loop count - 2 hex digits to print
Illustration Example print_digit: cmp AL, 9 jg A_to_F add AL, ’ 0’ jmp skip A_to_F: add AL, ’A’-10 skip: Put. Ch AL mov AL, AH and AL, 0 FH loop print_digit newline Put. Str query_msg Get. Ch AL cmp AL, ’Y’ jne read_char done: . EXIT ; if greater than 9 ; convert to A through F digits ; otherwise, convert to 0 through 9 ; subtract 10 and add ’A’ to convert to A through F ; write the first hex digit ; restore input character in AL ; mask off the upper half-byte ; move to next line (macro) ; query user whether to terminate ; read response ; if response is not ’Y’ ; read another character ; otherwise, terminate program
- Slides: 17