Character Translation Methods



Similar documents
How To Use A Computer With A Screen On It (For A Powerbook)

8. MACROS, Modules, and Mouse

Faculty of Engineering Student Number:

Using Heap Allocation in Intel Assembly Language

by Kip Irvine. Last update: 12/11/2003

Computer Organization and Assembly Language

MACHINE ARCHITECTURE & LANGUAGE

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Chapter 4: Computer Codes

ASSEMBLY PROGRAMMING ON A VIRTUAL COMPUTER

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

The Hexadecimal Number System and Memory Addressing

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Chapter 7 Assembly Language

So far we have considered only numeric processing, i.e. processing of numeric data represented

Appendix C: Keyboard Scan Codes

Mail 2 ZOS FTPSweeper

Arduino Lesson 5. The Serial Monitor

PART B QUESTIONS AND ANSWERS UNIT I

MarshallSoft AES. (Advanced Encryption Standard) Reference Manual

Remote login (Telnet):

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Complete 8086 instruction set

2011, The McGraw-Hill Companies, Inc. Chapter 3

Reading Delimited Text Files into SAS 9 TS-673

King Fahd University of Petroleum and Minerals. College of Computer Science and Engineering. Computer Engineering Department COE 205

Configuring Serial Terminal Emulation Programs

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

Lab Experience 17. Programming Language Translation

Technical Paper. Reading Delimited Text Files into SAS 9

MS-DOS, PC-BIOS, and File I/O Chapter 13

32-Bit Workload Automation 5 for Windows on 64-Bit Windows Systems

Number Representation

Chapter 11: Input/Output Organisation. Lesson 06: Programmed IO

The Answer to the 14 Most Frequently Asked Modbus Questions

ASSEMBLY LANGUAGE PROGRAMMING (6800) (R. Horvath, Introduction to Microprocessors, Chapter 6)

A Tiny Guide to Programming in 32-bit x86 Assembly Language

Virtual Integrated Design Getting started with RS232 Hex Com Tool v6.0

Lab 6 Introduction to Serial and Wireless Communication

NØGSG DMR Contact Manager

SUDT AccessPort TM Advanced Terminal / Monitor / Debugger Version 1.37 User Manual

X86-64 Architecture Guide

HP-UX Essentials and Shell Programming Course Summary

CENTRONICS interface and Parallel Printer Port LPT

System Calls and Standard I/O

TCP/IP Networking, Part 2: Web-Based Control

RS-485 Protocol Manual

Compiler Construction

Secret Communication through Web Pages Using Special Space Codes in HTML Files

Application Unit, MDRC AB/S 1.1, GH Q R0111

WinHLLAPI Language Reference

Tamper protection with Bankgirot HMAC Technical Specification

BCD (ASCII) Arithmetic. Where and Why is BCD used? Packed BCD, ASCII, Unpacked BCD. BCD Adjustment Instructions AAA. Example

Lecture 22: C Programming 4 Embedded Systems

Allen-Bradley. Bar Code. 2-D Hand-Held. Programming Guide. Bar Code. Scanners. (Cat. No HTG-4)

Continuous Integration Part 2

Streaming Lossless Data Compression Algorithm (SLDC)

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

Cyber Security Workshop Encryption Reference Manual

Yubico YubiHSM Monitor

User Manuals. Connection to Siemens S5 PU (AS511) Part Number: Version: 2. Date:

Version 1.5 Satlantic Inc.

Hexadecimal Object File Format Specification

Software Developer's Manual

Comp 255Q - 1M: Computer Organization Lab #3 - Machine Language Programs for the PDP-8

Cleo Host Loginid Import Utility and Cleo Host Password Encryption Utility

Notes on Assembly Language

64-Bit NASM Notes. Invoking 64-Bit NASM

2 ASCII TABLE (DOS) 3 ASCII TABLE (Window)

ND48-RS ASCII A2.04 Communication Protocol

Intel Hexadecimal Object File Format Specification Revision A, 1/6/88

Variables, Constants, and Data Types

Nokia E90 Communicator Using WLAN

TAP Interface Specifications

HOST Embedded System. SLAVE EasyMDB interface. Reference Manual EasyMDB RS232-TTL. 1 Introduction

EE 261 Introduction to Logic Circuits. Module #2 Number Systems

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Sources: On the Web: Slides will be available on:

COMPUTERS ORGANIZATION 2ND YEAR COMPUTE SCIENCE MANAGEMENT ENGINEERING JOSÉ GARCÍA RODRÍGUEZ JOSÉ ANTONIO SERRA PÉREZ

Laser Barcode Scanner User s Manual

Managed File Transfer with Universal File Mover

Illustration 1: Diagram of program function and data flow

Lab 4: Socket Programming: netcat part

Interface and Simulation of a LCD Text Display

IBM Sterling Connect:Enterprise for z/os

Introduction. Application Security. Reasons For Reverse Engineering. This lecture. Java Byte Code

Tutorial on C Language Programming

The use of binary codes to represent characters

LC-3 Assembly Language

MONETA.Assistant API Reference

Transport Layer Protocols

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

Introduction to MIPS Programming with Mars

Basic Import Utility User Guide

Merchant Card Payment Engine

INPUT AND OUTPUT STREAMS

The programming language C. sws1 1

Windows PowerShell Essentials

DBF Chapter. Note to UNIX and OS/390 Users. Import/Export Facility CHAPTER 7

Transcription:

Supplement to: Irvine, Kip R. Assembly Language for Intel-Based Computers, 4th Edition. This file may be duplicated or printed for classroom use, as long as the author name, book title, and copyright notice remain unchanged. Character Translation Methods One task that assembly language programs handle best is character translation. There are many computer applications for this. The rapidly expanding field of data encryption is one, where data files must be securely stored and transmitted between computers while their contents are kept secret. Another application is data communications, where keyboard and screen codes must be translated in order to emulate various terminals. Often, we need to translate characters from one encoding system to another from ASCII to EBCDIC, for example. A critical factor in each of these applications is speed, which just happens to be a feature of assembly language. The XLAT Instruction The XLAT instruction adds AL to EBX and uses the resulting offset to point to an entry in an 8- bit translate table. This table contains values that are substituted for the original value in AL. The byte in the table entry pointed to by EBX + AL is moved to AL. The syntax is XLAT [tablename] Tablename is optional because the table is assumed to be pointed to by EBX (or BX, in Realaddress mode). Therefore, be sure to load BX with the offset of the translate table before invoking XLAT. The flags are not affected by this instruction. The table can have a maximum of 256 entries, the same range of values possible in the 8-bit AL register. Example Let's store the characters representing all 16 hexadecimal digits in a table: table BYTE '0123456789ABCDEF' The table contains the ASCII code of each hexadecimal digit. If we place 0Ah in AL with the thought of converting it to ASCII, we can set EBX to the table offset and invoke XLAT. The instruction adds EBX and AL, generating an effective address that points to the eleventh entry in the table. It then moves the contents of this table entry to A: mov al,0ah mov ebx,offset table xlat ; index value ; point to table ; AL = 41h, or 'A' (c) Pearson Education, 2003.

2 Character Translation Methods Character Filtering One of the best uses of XLAT is to filter out unwanted characters from a stream of text. Suppose we want to input a string of characters from the keyboard and echo only those with ASCII values from 32 to 127. We can set up a translate table, place a zero in each table position corresponding to an invalid character, and place 0FFh in each valid position: validchars BYTE 32 DUP(0) ; invalid chars: 0-31 BYTE 96 DUP(0FFh) ; valid chars: 32-127 BYTE 128 DUP(0) ; invalid chars: 128-255 The following Xlat.asm program includes code that inputs a series of characters and uses them to look up values in the validchars table: TITLE Character Filtering (Xlat.asm) ; This program filters input from the console ; by screening out all ASCII codes less than ; 32 or greater than 127. Uses INT 16h for ; direct keyboard input. INCLUDE Irvine32.inc INPUT_LENGTH = 20.data validchars BYTE 32 DUP(0) ; invalid chars: 0-31 BYTE 96 DUP(0FFh) ; valid chars: 32-127 BYTE 128 DUP(0) ; invalid chars: 128-255.code main PROC mov ebx,offset validchars mov ecx,input_length getchar: call ReadChar ; read character into AL mov dl,al ; save copy in DL xlat validchars ; look up char in AL or al,al ; invalid char? jz getchar ; yes: get another mov al,dl call WriteChar ; output character in AL loop getchar call Crlf exit main ENDP END main

3 If XLAT returns a value of zero in AL, we skip the character and jump back to the top of the loop. (When AL is ORed with itself, the Zero flag is set if AL equals 0). If the character is valid, 0FFh is returned in AL, and we call WriteChar to display the character in DL. Character Encoding The XLAT instruction provides a simple way to encode data so it cannot be read by unauthorized persons. When messages are transferred across telephone lines, for instance, encoding can be a way of preventing others from reading them. Imagine a table in which all the possible digits and letters have been rearranged. A program could read each character from standard input, use XLAT to look it up in a table, and write its encoded value to standard output. A sample table is as follows: codetable LABEL BYTE BYTE 48 DUP(0) BYTE '4590821367' ; ASCII codes 48-57 BYTE 7 DUP (0) BYTE 'GVHZUSOBMIKPJCADLFTYEQNWXR' BYTE 6 DUP (0) BYTE 'gvhzusobmikpjcadlftyeqnwxr' BYTE 133 DUP(0) Certain ranges in the table are set to zeros; characters in these ranges are not translated. The $ character (ASCII 36), for example, is not translated because position 36 in the table contains the value 0. Sample Program Let s take a look at a character encoding program that encodes each character read from an input file. When running the program, one can redirect standard input from a file, using the < symbol. For example: encode < infile The program output appears on the screen. Output can also be redirect to a file: encode < infile > outfile The following example shows a line from an input file that has been encoded: This is a SECRET Message Ybmt mt g TUHFUY Juttgou (read from input file) (encoded output) The program cannot use the keyboard as the standard input device, because it quits looking for input as soon as the keyboard buffer is empty. The user would have to type fast enough to keep the keyboard buffer full. Given some time and effort, a simple encoding scheme like this can be broken. The easiest way to break the code is to gain access to the program itself and use it to encode a known message. But if you can prevent others from running the program, breaking the code takes more time. Another way to discourage code breaking is to constantly change the code. In World War

4 Character Translation Methods II, for example, pilots carried a code book for translating messages, so that message encryption could be varied on a day-to-day basis. TITLE Character Encoding Program (Encode.asm) ; This program reads an input file and encodes ; the output using the XLAT instruction. ; To run it, redirect input at the Command prompt: ; ; encode < input.txt ; ; Implemented as a 16-bit application because we can ; use INT 21h function 6 to input characters without ; waiting. See Section 13.2.3 for details. ; Last update: 06/01/02 INCLUDE Irvine16.inc.data codetable LABEL BYTE BYTE 48 DUP(0) BYTE '4590821367 '; ASCII codes 48-57 BYTE 7 DUP (0) BYTE 'GVHZUSOBMIKPJCADLFTYEQNWXR' BYTE 6 DUP (0) BYTE 'gvhzusobmikpjcadlftyeqnwxr' BYTE 133 DUP(0).code main PROC mov ax,@data mov ds,ax mov bx,offset codetable getchar: push bx mov ah,6 mov dl,0ffh int 21h pop bx jz quit mov dl,al xlat cmp al,0 je putchar mov dl,al ; input character, don't wait ; call DOS ; quit, no input waiting ; save char in DL ; translate the character ; translatable? ; no: write it as is ; yes: move new char to DL putchar:

5 mov al,dl call WriteChar jmp getchar ; character is in DL ; write AL to standard output ; get another char quit: exit main ENDP END main

6 Character Translation Methods